html - Conditionally opening page on form submit with JavaScript -


i'm trying set conditional page load on form submit, following "if user selects [x] option, send them [x] page" logic. i'm not familiar javascript, seemed easiest way go it.

here's html:

<form id="page1" action="javascript:openwindow()" method="post">     <fieldset id="mainselection">             <label><input type="radio" class="radio-button" value="a" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="b" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="c" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="d" name="sel1"> text</label><br />     </fieldset>     <button type="submit" value="next" id="submitbutton">next</button> </form> 

and here script:

<script type="text/javascript" language="javascript">         function openwindow() {         var choice = document.getelementbyclassname("radio-button").value             if (choice == a) {                 window.open('http://www.website.net');             }             else if (choice == b) {                 window.open('http://www.website.net');             }             else if (choice == c) {                 window.open('http://www.website.net');             }             else {                 window.open('http://www.website.net');             }         } </script> 

what not doing correctly here?

i got work few changes.

  • add onsubmit="openwindow()" form element

  • changing how selected element

  • quotes around conditional ('a', 'b', etc)

here's link codepen: http://codepen.io/anon/pen/qajxh

so looks this:

<form onsubmit="openwindow()" id="page1" action="javascript:openwindow()" method="post">     <fieldset id="mainselection">             <label><input type="radio" class="radio-button" value="a" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="b" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="c" name="sel1"> text</label><br />             <label><input type="radio" class="radio-button" value="d" name="sel1"> text</label><br />     </fieldset>     <button type="submit" value="next" id="submitbutton">next</button> </form> 

with js this:

function openwindow() {     var choice = document.getelementbyid("page1");     choice = choice.sel1.value;      if (choice == 'a') {         window.open('http://www.website.net');     }     else if (choice == 'b') {         window.open('http://www.website.net');     }     else if (choice == 'c') {         window.open('http://www.website.net');     }     else {         window.open('http://www.website.net');     } }  

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -