Leo Vorontsov
Leo Vorontsov

Reputation: 83

JavaScript redirect based on option selected

I need some help with the JavaScript redirect. I have the following code for the form, but it is not working.

<form name="getstarted" width="500">  
    <p>
  
        <select name="trade" id="trade">  
          <option selected="selected" value = "">Please Select License Trade</option>  
          <option value = "A">A-General Engineering</option>  
          <option value = "B">B-General Building</option>  
          <option value = "C2">C2 - Insulation and Acoustical</option>  
        </select>  

    </p>

    <p>Do you currently hold an <strong>active</strong> contractors license in California? <br /> <br />
        <INPUT TYPE="radio" id="questions" NAME="questions" VALUE="y">Yes
        <INPUT TYPE="radio" id="questions" NAME="questions" VALUE="n">No</p>

     <input type="button" value="Next" onClick = "direction()" />  

</form>

<script language="javascript">  
function direction(value)     
{  
    if((trade == "A") && (questions == "y"))  
    {window.location = "http://localhost:8080/Math/page1.html";}  
      
    else{window.location = "http://localhost:8080/Math/page2.html";}  
}  
</script>
<script type="text/javascript">
        $(document).ready(function(){
            $(".icon a.tooltips").easyTooltip();
        });
</script>

I am trying to have this code redirect users based on two different options selected.

Upvotes: 0

Views: 808

Answers (1)

Sergio
Sergio

Reputation: 28845

Try this:

function direction(value) {
    var trade = $("#trade").val();
    var questions = $(".questions:checked").val();
    if ((trade == "A") && (questions == "y")) {
        window.location = "http://localhost:8080/Math/page1.html";
    } else {
        window.location = "http://localhost:8080/Math/page2.html";
    }
}

Demo here

What I added:

var trade = $("#trade").val();
var questions = $(".questions:checked").val();

and removed the duplicate ID and used a class instead:

    <INPUT TYPE="radio" class="questions" NAME="questions" VALUE="y"/>Yes
    <INPUT TYPE="radio" class="questions" NAME="questions" VALUE="n"/>No

Upvotes: 1

Related Questions