javascript that clicks a button

0
Good afternoon, I'm trying to use a javascript that clicks a button on the datagrid, but I keep getting an error. The grid has the id=grid1 and the button id=selectAllButton1 Does anyone have any ideas ?  
asked
2 answers
2

Like Bill suggested the javascript is probably executed before the page is fully loaded. Also you claim to have a button with a certain id, but id's are set by Mendix. Set a unique class name on your button (at least, unique for the page it's in), or set the name, which will result in a class "mx-name-${yourCustomName}". Then you can check using an interval if the element is there yet and if so, click it. This code snippet should try thirty times every 200 ms, then die. Make sure you edit the buttons class name in the code on line 7. This is pure javascript btw, no jQuery needed.

 

var iterationCount = 0
var subIntervalID = setInterval(function() {
    iterationCount++;
    if(iterationCount > 30) {
       clearInterval(subIntervalID)
    }
    var theButton = document.getElementsByClassName("myUniqueButtonClass")[0];
    if (theButton) {
        theButton.click();
        clearInterval(subIntervalID);
    }
}, 200);

 

 

 

answered
2

Hi Pedro,

This is most likely due to the order of page load.  Try adding a delay or ready to your JavaScript function.  I built something similar in a datagrid2 about a year ago.  We ended removing it because it never worked consistently, but I believe the code looked like this: 
 

    $(document).ready(function() {
        $(".btn1").click(function(){
           $(".btn2").trigger("click");
           return false;
         });
     });

answered