0
$('#selectclassid').trigger("change");
    $('#selectclassid').on('change', function() {
});

I have this format for my onchange manual trigger of on change but the problem is it is not firing. I am wondering because this is the format I see in tutorials. Could there be anything wrong in the format of on change. I am hoping that it will fire on change when the page is loaded. By the way this select option is in a div that is hidden on load.But will show on button click if this event is related to the problem. Any idea is appreciated

Pekka
  • 1,075
  • 2
  • 10
  • 17

3 Answers3

5

Hm, you seem to be triggering the event before you attach it - try it the other way around

 $('#selectclassid').on('change', function() {
     console.log("fired")
 });

 $('#selectclassid').trigger("change");
nicholaswmin
  • 21,686
  • 15
  • 91
  • 167
  • i have a question when my input is readonly even if the pattern is like this the onchange is not firing any idea why? – Pekka Apr 27 '15 at 03:32
  • Read [this](http://stackoverflow.com/questions/14332972/fire-onchange-event-for-textbox-when-readonly-is-true) - Try googling some questions first - Most of the time it's faster for you to get an answer – nicholaswmin Apr 27 '15 at 04:18
0

https://jsfiddle.net/ce5rg9mt/

$( document ).ready(function() {




$("#selectclassid").on('change', function() {
    alert('The option with value ' + $(this).val());
});


     $( "#selectclassid" ).trigger( "change" );

});
Bryan Dellinger
  • 4,724
  • 7
  • 33
  • 79
-2

Wrap your code inside of the callback function like so:

$('#selectclassid').on('change', function(){
    //code here
});

And then call the trigger afterwards:

$('#selectclassid').trigger('change');