javascript - Drop down selection using HTML/jQuery -


i want build drop down menu second selection displayed if first selection data belongs specific category.

as can see below, first selection countries. if country selected has states, second drop down selection displayed, containing states of country.

1)is there tag (in code "xyz") can use separate countries in "state" , "no-state" categories? if there is, how can read value of "xyz" tag?

2) if use the:

<option class="no-state" value="germany">germany</option> 

and use jquery read it give me value germanyspain (which correct not want)

$('.no-state').val(); 

html part

<div id="country">  <select>      <option xyz="no-state" value="germany">germany</option>      <option xyz="state" value="usa">usa</option>      <option xyz="no-state" value="spain">spain</option>  </select> </div>  <div id="state" style="display:none" >  <select>      <option value="utah">utah</option>      <option value="new york">new york</option>      <option value="california">california</option>  </select> </div> 

jquery part

$(document).ready(function(){     $('#country').change(function() {         if (the value of "xyz" tag === 'no-state')          {         $('div#state').hide();        }        else       {         $('div#state').show();       }     }); }); 

what can address issue?

thanks.

added variable keep if country has states or not according custom attribute xyz

js

$(document).ready(function(){     $('#country').change(function() {        var hasstates = $(this).find("option:selected").attr("xyz");        if (hasstates == 'no-state')          {         $('div#state').hide();        }        else       {         $('div#state').show();       }     }); }); 

fiddle


Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -