-1

I have selectbox, after page loads selected item changed(via ajax call) and I need to catch this event, so I added this;

 $(document).ready(function() {
                document.getElementById("mySelectBox").addEventListener("change", function() {
                    debugger
                }, true);
            })

and expected when selected item changed async it triggers and step into the function inside listener but its not...

This can be solved using setTimeOut function, but its ugly and as you know there is possible side effects.

setTimeout(function(){*business logic*},2000)
TyForHelpDude
  • 4,828
  • 10
  • 48
  • 96

3 Answers3

1

If you are adding elements dynamically by using AJAX, use the on() method. Let's say there is a div (the-parent-div) which contains #mySelectBox:

$(document).ready(function() {
    $('the-parent-div').on('change', '#mySelectBox', function(){
         //Your actions go here
    });
});

UPDATE: You might be referring to the async nature of ajax calls. See this Question and the accepted Answer:

....... The A in Ajax stands for asynchronous . That means sending the request (or rather receiving the response) is taken out of the normal execution flow. In your example, $.ajax returns immediately and the next statement, return result;, is executed before the function you passed as success callback was even called. .....

Victor M Perez
  • 2,185
  • 3
  • 19
  • 22
1

There is a missing ) at the end of the code

gldanoob
  • 762
  • 1
  • 7
  • 18
  • 1
    Questions where the problem is caused by a typo are off-topic. They should be closed and not answered. When you have more reputation you'll be able to comment on any question and vote to close them. In the meantime, please don't *answer* off-topic questions. – Quentin Feb 22 '18 at 08:33
1

First, you have a typo in your code. You forgot the ) at the end, but I assume that is a transcription error.

The change is event is fired when the user interface is used to change the form control.

You said "when selected item changed async" which implies that you are changing the form control using JavaScript and not by a user interaction event.

This won't trigger the event.

You can either call the function manually…

 $(document).ready(function() {
     function changefunction() {
         debugger
     }
     document.getElementById("mySelectBox").addEventListener("change", changefunction, true);
     do_something_to_change_mySelectBox();
     changefunction.call(document.getElementById("mySelectBox"));
 });

… or make use of jQuery's event abstraction features. This would require that you bind the event handle using jQuery though.

 $(document).ready(function() {
     $("#mySelectBox").on("change", function() {
         debugger
     });
     do_something_to_change_mySelectBox();
     $("#mySelectBox").trigger("change");

 });
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Oops. Typo. Fixed. And live demo: http://jsbin.com/xelasutade/1/edit?html,js,output (and make sure your debugging tools are open!). – Quentin Feb 22 '18 at 08:54