How can I call the change select box when the select value is changed using jQuery?


Jayababu

I have a select box like this

 <select class="cars" onChange="carName()">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

If you want to change the dropdown value, the carNamefunction will be called and run as expected. If suppose I use jQuery to change the value like this

$('.cars').val('Audi');

I want to carName()call this function, what should I do?

Sapar

You need to do the event programmatically.trigger()

Executes all handlers for the given event type and actions attached to matching elements.

Also, the value is case sensitive, so use the correct value. replace audiwithAudi

code ,

$('.cars').val('audi').trigger('change'); 

Also since you are using bind event, use this

$('.cars').on('change', carName); //Or, $('.cars').change(carName);

function carName() {
  alert('In carName function');
}

$(document).ready(function() {
  $('.cars').val('audi').trigger('change');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="cars" onChange="carName()">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

Related


How can I set the first option on a select box using jQuery?

Anish: I have two HTML selectboxes. I need to reset a box selectwhen I select another box. <select id="name" > <option value="">select all</option> <option value="1">Text 1</option> <option value="2">Text 2</option> <option value="3">Text 3</op

How can I set the first option on a select box using jQuery?

Anish: I have two HTML selectboxes. I need to reset a box selectwhen I select another box. <select id="name" > <option value="">select all</option> <option value="1">Text 1</option> <option value="2">Text 2</option> <option value="3">Text 3</op

How can I set the first option on a select box using jQuery?

Anish: I have two HTML selectboxes. I need to reset a box selectwhen I select another box. <select id="name" > <option value="">select all</option> <option value="1">Text 1</option> <option value="2">Text 2</option> <option value="3">Text 3</op

Ionic 2: How to call method when select value is changed

CMA I'm new to ionic 2 and I read the ionic 2 documentation and thought the code would work. It should return the current selection value after the change, and print it to the console. page.html <ion-select #C ionChange="onChange(C.value)">

Ionic 2: How to call method when select value is changed

CMA I'm new to ionic 2 and I read the ionic 2 documentation and thought the code would work. It should return the current selection value after the change, and print it to the console. page.html <ion-select #C ionChange="onChange(C.value)">

Ionic 2: How to call method when select value is changed

CMA I'm new to ionic 2 and I read the ionic 2 documentation and thought the code would work. It should return the current selection value after the change, and print it to the console. page.html <ion-select #C ionChange="onChange(C.value)">

Ionic 2: How to call method when select value is changed

CMA I'm new to ionic 2 and I read the ionic 2 documentation and thought the code would work. It should return the current selection value after the change, and print it to the console. page.html <ion-select #C ionChange="onChange(C.value)">