The Ferydra
The Ferydra

Reputation: 13

$('select').change(function but on only one specific dropdown? (HTML, Javascript)

I'm trying to do that this code:

$(document).ready(function () {
$('select').change(function () {
    alert(this.value);
});

Alerts a message but only when one of the various dropdowns onscreen is selected. But it works whenever I pick ANY dropdown. How can I do it so that it only works with one specific dropdown?

Upvotes: 1

Views: 91

Answers (1)

Lee Taylor
Lee Taylor

Reputation: 7984

Simply give your respective select tag an id, then use that id for your selector in your jQuery:

$('#select1').change(function () 
{
    alert(this.value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select id="select1">
  <option value="">--Please Choose--</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

<select id="select2">
  <option value="">--Please Choose--</option>
  <option value="A">Apple</option>
  <option value="B">Banana</option>
  <option value="C">Cherry</option>
</select>

Upvotes: 1

Related Questions