1

I just starting with JavaScript, focusing in DOM, because I have to automate some human jobs in our web services.

I'm trying to to click on a button, using JavaScript code, but I can't access the click() function to this button. It's probably because, this is not actually a declared button, the button is inside a <div><span><a> tag, using role=button.

Follows part the code in HTML:

<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
  <span class="ui-dialog-title" id="ui-dialog-title-jobCardDetail">text..</span>
  <a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button">
    <span class="ui-icon ui-icon-closethick">close</span>
  </a>
  ::after
</div>

How can I click on it?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • "I can't access the `click()` function to this button". How are you trying to do this? There is a `click()` function on an anchor element as well... – Heretic Monkey May 05 '20 at 19:52
  • Does this answer your question? [How can I trigger a JavaScript event click](https://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click) – Heretic Monkey May 05 '20 at 19:54

1 Answers1

-1

You can query the element and use click() over it. Here I have queried using the classNames.

Here I have added an event listener over the button just to show that a click has occurred over the button.

const btn = document.querySelector(".ui-dialog-titlebar-close.ui-corner-all");
// test code
btn.addEventListener('click', () => {
  console.log('Click Check!');
});
// test code
btn.click();
<div class="ui-dialog-titlebar ui-widget-header ui-corner-all ui-helper-clearfix">
<span class="ui-dialog-title" id="ui-dialog-title-jobCardDetail">text..</span>
<a href="#" class="ui-dialog-titlebar-close ui-corner-all" role="button">
<span class="ui-icon ui-icon-closethick">close</span>
</a>
::after
</div>
Ashish Ranjan
  • 12,760
  • 5
  • 27
  • 51