c.yoder
c.yoder

Reputation: 7

Conditional testing in Javascript

I am trying to conditionally have a <div> be displayed if the numeric input is 18 and above and the YO option is selected. Any assistance would be greatly appreciated. Code below.

<input type=numeric id=ageSelf ng-model="ageSelf" style="color:black">
    <select ng-model="ageUnit" style="color:black">
        <option selected value=YO>YO</option>
        <option>MO</option>
        <option>WO</option>
    </select><br>

<script type=text/javascript>
  var jq = $.noConflict(); //I am also using angularjs hence the variable
  jq(document).ready(function(){
    jq("#ageSelf").change(function(){
      //conditional for ageSelf
      if(jq("#ageSelf").val >= "18"){
        if(jq("#ageUnit") == YO){
          jq("#over18").show();
        }
      } else {
        jq("#under18").show();
      }         
    });     
});

Upvotes: 0

Views: 52

Answers (1)

adeneo
adeneo

Reputation: 318262

It's val() as it's a function

if( jq("#ageSelf").val() >= "18" ) { ...

and you're checking strings, not numbers, you probably want

var val = parseInt( jq("#ageSelf").val(), 10 );

if( val() >= 18 ) { ...    

and YO should be quoted, both in the HTML and in the JS

<select id="ageUnit" ng-model="ageUnit" style="color:black">
    <option value="YO" selected>YO</option>

and

if( jq("#ageUnit").val() == "YO" ) { ...

all together

jQuery(function($) {
    $("#ageSelf").on('change', function(){

        var val = parseInt( $("#ageSelf").val(), 10 );

        $("#over18").toggle( val >= 18 && $("#ageUnit").val() === "YO" );

    });     
});

Upvotes: 3

Related Questions