Javascript detect open accordion

Hi,

I need to detect whether a particular accordion item displays its contents correctly on the browser showing the page in question. In order to do that I am using javascript/jQuery with a mouseup() event handler.

$("div#mydiv").find("a.x-accordion-toggle").mouseup(function(){
    var thisvar = this;
   setTimeout(function(){
      if($(thisvar).attr("class") != "x-accordion-toggle collapsed"){
       //....actions to take to check display is ok
    },300);
});

Since the accordion item will take some time after mouseup before the contents are displayed, I need to put in a setTimeout() function – however, this feels slightly inelegant and you can never be sure how much the time lag will vary between individual machines/browsers.

Is there a way to specifically detect the event that the accordion opening is complete?

Hi there,

You can use the setInterval function to repeat the test of the opened view with a shorter time period such as 100 milliseconds.

Then you need to clear the interval after the condition is met and you start the action you want to do.

For more information:

https://www.w3schools.com/jsref/met_win_setinterval.asp

https://www.w3schools.com/jsref/met_win_clearinterval.asp

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.