javascript - How to open different URLs by checkbox? -


this html , javascript code

<input type="text" id="text" />  <input type="button" id="btn" value="submit" onclick="javascript: window.open('http://example.com/' + document.getelementbyid('text').value);" /> 

if input anytext in form box , click on submit button, open http://example.com/anytext. want add 3 checkbox named page1 page2 , page3 under form. if click on checkbox page1 , click on submit button, open http://example.com/page1/anytext

how can it?

you create function processes value of checkboxes , returns string need insert window.open call. this:

onclick="javascript: window.open('http://example.com/' +getradiovalue() + document.getelementbyid('text').value);" 

also, note snippet won't redirect when click submit due sandbox restrictions on it, can see error in browser console , page code attempts redirect to.

function getradiovalue () {    var cb1 = document.getelementbyid("cb1");    var cb2 = document.getelementbyid("cb2");    var cb3 = document.getelementbyid("cb3");        if(cb1.checked) {      return "page 1/";    } else if(cb2.checked) {      return "page 2/";    } else if(cb3.checked) {      return "page 3/";    } else {      return "";    }  }
<input type="text" id="text" />  <br/>  <label>  <input type="checkbox" value="page 1" id="cb1" />  page 1  </label>  <br/>  <label>  <input type="checkbox" value="page 2" id="cb2" />  page 2  </label>  <br/>  <label>  <input type="checkbox" value="page 3" id="cb3" />  page 3  </label>  <br/>  <input type="button" id="btn" value="submit" onclick="javascript: window.open('http://example.com/' +getradiovalue() + document.getelementbyid('text').value);" />


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -