What is the Dojo Equivalent to Button Click

2.5k views Asked by At

In JavaScript - If I want to fire a button I do this -

<button type = 'button' id='myButton'>HiddenButton</button>
<script> 
  function callAutoClick(){ 
         document.getElementById('myButton').click(); 
  }
</script>

When I want to fire this button click, say, onChange of a text field -

<input type= 'text' onchange ='callAutoClick()'/>

I am not able to do the same in DOJO. I have found a solution using Javascript -

var divId = document.getElementById('myDivID');
divId.getElementsByTagName("button")[0].click();

But I don't want to have the dependency with the DivID. Is it possible to click a button just knowing the button's controlID. All I could find online were methods to define an OnClick() using dojo for a button and not clicking the button itself.

Plus I am designing the page with a BPM Tool so on including sections the DivID changes. When I open the page in FireBug I can see this -

<div id="div_1_1_2_1" class="Button CoachView CoachView_invisible CoachView_show" data-ibmbpm-layoutpreview="vertical" data-eventid="boundaryEvent_2" data-viewid="Hidden_Cancel" data-config="config23" data-bindingtype="" data-binding="" data-type="com.ibm.bpm.coach.Snapshot_2d5b8abc_ade1_4b8c_a9aa_dfc746e757d8.Button">
<button class="BPMButton BPMButtonBorder" type="button">Hidden_Cancel</button>
</div>

If you guys could suggest me a way to access the DOM object using the data-viewId also it would cater to my need.

Thanks in advance :)

1

There are 1 answers

0
Elad On BEST ANSWER

You can use dojo/query:

function progClick() {
  require(["dojo/query"], function(query) {
    query("div[data-viewid=myViewId] > button").forEach(function(node) {
      node.click();
    });
  });
}
<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/dojo/1.10.1/dojo/dojo.js" data-dojo-config="async: true"></script>
  <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/dojo/1.10.1/dijit/themes/claro/claro.css">
</head>

<body class="claro">

  <div id="everChangingId" data-viewid="myViewId">
    <button type="button" onclick="alert('my button got clicked!')">My Button</button>
  </div>
  
  <hr/>
  
  <button type="button" onclick="progClick()">Programatically Click My button</button>

</body>
</html>