0

How to select multiple id according to their value (select tag) with jquery.

<select id="year">
    <option value="1900">
</select>

<select id="year2">
   <option value="1950">
</select>

I tried several solutions with querySelectorAll, it does not work

Jaymin Panchal
  • 2,797
  • 2
  • 27
  • 31
clement
  • 43
  • 1
  • 5

2 Answers2

0

You do it the same way you'd do it in a CSS rule:

$("#year, #year2").whatever();

You can in general separate distinct CSS selectors by commas, meaning that you want the aggregate of what's matched by each of them.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

If you are trying to select only the select elements that have value == 1950 for example, you can use the filter function:

selected = $('select').filter(function() {
  return $(this).val() == 1950;
});
selected.css('background', 'red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="year">
<Option value = "1900">1900</option>
</select>
<br />
<select id="year2">
<option value = "1950">1950</option>
</select>
Dekel
  • 60,707
  • 10
  • 101
  • 129