Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add click event on an option in html select tag [duplicate]

I have a html dropdown defined as

<select name="evidence_selected"id="evidence_selected">
<option id="a">A</option>
<option id="b">B</option>
<option id="c">C</option>
<option id="new">New</option>
</select>

I want to fire an on click event on "new" so that when a user clicks on it, a free form appears where they enter some other value that is not on dropdown. The id of the form is "new_value". I tried

$("#new").click(function(){
  $("new_value").show();

});

It seems the click event wont fire. Any help(with code snippet) will be highly appreciated. Regards

like image 697
user3670066 Avatar asked Oct 31 '22 23:10

user3670066


1 Answers

Selects use the change event

$('select').change(function () {
  if ($(this).val() === 'New') {
    // Handle new option
  }
});

This will trigger any time any of the options are selected.

like image 193
JakeSidSmith Avatar answered Nov 08 '22 05:11

JakeSidSmith