Javascript: Click a button on page load

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
marisoft
User
Posts: 209

Javascript: Click a button on page load

Post by marisoft »

Hello,

I would like to automatically click the export button - that is set to use export to CSV only - when the page is loaded.
The javascript will be inserted in
"Startup Script" under "Client Scripts" -> "Table-Specific" -> "List Page"

If button is defined like this:
<button href="images/car.jpg" id="myButton">
Here is the Button to be clicked
</button>

Then javascript would be this:
<script>
$(document).ready(function(){
document.getElementById("myButton").click();
});
</script>

But what is the id of the export button when this is generated in PhpMaker2017:
<div class="btn-group ewButtonGroup">
<a title="" class="btn btn-default ewExportLink ewCsv" href="sr_logbooklist.php?export=csv" data-original-title="CSV" data-caption="CSV">
<span class="icon-csv ewIcon" data-caption="Export til CSV" data-phrase="ExportToCsv">
</span>
</a>
</div>

tia
/Poul


arbei
User
Posts: 9292

Post by arbei »

You can use jQuery and a proper selector (not just by id) to select the element and trigger the click() event in Startup Script.

Search "jQuery Selector" in Google search for more information on selectors.


marisoft
User
Posts: 209

Post by marisoft »

Done some reading and found this method - but cannot get it to work:

$("document").ready(function() {
setTimeout(function() {
$('.btn btn-default ewExportLink ewCsv').click();
},10);
});
</script>

Tried some variants like these that also has been suggested:
$('.btn btn-default ewExportLink ewCsv')[0].trigger('click');
$('.btn btn-default ewExportLink ewCsv')[0].trigger('click');
$('.btn btn-default ewExportLink ewCsv')[0].click();

Also tried variant with double qoutes like:
$(".btn btn-default ewExportLink ewCsv").click();

Any clues what could be wrong ?

/Poul


mobhar
User
Posts: 11660

Post by mobhar »

Change:
$('.btn btn-default ewExportLink ewCsv').click();

to:
$('.btn.btn-default.ewExportLink.ewCsv').click(); // No space before dots if selecting the same element

In other words, each time you use the class name, make sure you have already dot character as the prefix of its class name.


Post Reply