1

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?

1
  • 2
    Give your select tag an id, then use $('#myid') instead of $('select') Commented Apr 15, 2022 at 21:43

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.