Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drop down selection using HTML/jQuery

I want to build a drop down menu that the second selection will be displayed if the first selection data belongs to a specific category.

As you can see below, the first selection will be about COUNTRIES. If the country selected has states, then a second drop down selection will be displayed, containing the states of that country.

1)Is there a tag (in my code "xyz") that i can use it to separate the countries in "state" and "no-state" categories? If there is, how can i read the value of the "xyz" tag?

2) If i use the:

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

and then use the jQuery to read it it will give me the value GermanySpain (which is correct but not what i 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 is === 'no-state')  
       {
        $('div#state').hide();
       }
       else
      {
        $('div#state').show();
      }
    });
});

What can i do to address this issue?

Thanks.

like image 763
christostsang Avatar asked Jun 08 '14 10:06

christostsang


People also ask

How do I select a specific DropDownList using jQuery?

Syntax of jQuery Select Option$("selector option: selected"); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $("selector option: selected").

How do I make a drop down list in HTML?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).

How do you filter an HTML table based on drop down selected value?

In this case, you need to get all the data from the server into something you can run filtering logic on like a JavaScript object (usually using AJAX to get JSON data). After you got the data and filtered it you can use JavaScript to dynamically create the table rows and update the pagination data.


1 Answers

Added a variable to keep if a country has states or not according your 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

like image 50
Alex Char Avatar answered Oct 15 '22 16:10

Alex Char