jeudi 12 août 2021

JQUERY - Custom HTML Option Data for IF Statement for Chained Selection

I am trying to make the second dropdown selection dependent of the first, using jquery.

  • get "data-type" of first selection
  • if "data-type" == "String" trigger filter change of second selections "data-foo" containing value N

HTML Selections

<select id="first" name="first-selection" class="form-control">
 <option value="a" class="b" data-type="c">a</option>
</select>

<select id="second" name="second" class="form-control">
 <option value="n" data-foo="m">n</option>
</select>

I used to following code to check if I am able to get the "data-type" value and display it. But any attempt to get the data for an if statement failed so far.

$('#first').change(function () {
 var selected = $(this).find('option:selected');
 $('#type').html(selected.data('type'));
 }).change();

Aucun commentaire:

Enregistrer un commentaire