Tuesday, May 14, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  50] [ 3]  / answers: 1 / hits: 9225  / 4 Years ago, tue, january 7, 2020, 12:00:00

I have created a select2 dropdown with



    <select class=form-control select2  id=country>
<option value=1>India</option>
<option value=2>Aus</option>
<option value=3>usa</option>
</select>


Now i am selecting selected value via jquery trigger event



$('#country').val('1').trigger('change'); 


here, country gets selected with passed value but On this event i also want to trigger change event which is not triggering.



$(#country).change(function() {
alert('on country change');
});


If i change manually from select2 then the change event fires and it shows the alert but i want to fire the change event automatically when the country value changes from jquery. Any clues?



Thanks in advance.


More From » jquery

 Answers
1

You should trigger after change function, like this





$(#country).change(function() {
alert('on country change');
});

$('#country').val('1').trigger('change');

<script src=https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js></script>
<script src=https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/js/select2.min.js></script>
<link href=https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.10/css/select2.min.css rel=stylesheet/>



<select class=form-control select2 id=country style=width: 100px;>
<option value=1>India</option>
<option value=2>Aus</option>
<option value=3>usa</option>
</select>




[#5139] Friday, January 3, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Thu, Jan 2, 20, 00:00, 4 Years ago
;