Francis
Francis

Reputation: 147

trigger click automatically using jquery

I am trying to trigger click automatically after the page load. Here is the code

$("document").ready(function() {

    setTimeout(function() {

        $(".amcharts-export-menu-top-right").find('a').trigger('click');

    },1000);
});

But it is not working

Here is the entire code: https://jsfiddle.net/32g26a4d/

Upvotes: 1

Views: 2988

Answers (3)

xorspark
xorspark

Reputation: 16012

You should do this during the chart's rendered or animationFinished event if you have an animation. $("document").ready() doesn't ensure that the chart will be finished loaded as the chart creation process is asynchronous.

Add this to your makeChart call:

//if you have startDuration set to a value > 0:
AmCharts.makeChart("chartdiv", {
  // ...
  "listeners": [{
    "event": "animationFinished",
    "method": function() {
      $('.amcharts-main-div .amcharts-export-menu-top-right a')[0].dispatchEvent(new Event('click'));       
    }
  }]
});

//if you don't have startDuration set or it is set to 0:
AmCharts.makeChart("chartdiv", {
  // ...
  "listeners": [{
    "event": "rendered",
    "method": function() {
      setTimeout(function() {
        $('.amcharts-main-div .amcharts-export-menu-top-right a')[0].dispatchEvent(new Event('click'));       
      }, 500)      
    }
  }]
});

Updated fiddle

Upvotes: 0

Rana Ghosh
Rana Ghosh

Reputation: 4674

As you are using .export-main class for creating button using AmCharts you don't need to use any other class/find method for selecting that anchor button. Please follow below code::

$("document").ready(function() {
    setTimeout(function() {
        $(".export-main a")[0].click();
    },1000);
});

Working jsfiddle link:: link

Upvotes: 0

Leguest
Leguest

Reputation: 2137

You could try this

$("document").ready(function() {

 setTimeout(function() {

    $('.amcharts-main-div .amcharts-export-menu-top-right a')[0].dispatchEvent(new Event('click'))

   },1000);
 });

JSFiddle example

Upvotes: 4

Related Questions