javascriptclicksimulate

Simulate click with javascript with no element ID


<button 
   type="button" 
   class="blue" 
   data-events="click:onAddNote" 
   data-events-target="cpoe-LabResultDetailView"
>
   Add Note
</button>

I'm trying to simulate / prompt a click of this button when I press a shortcut key sequence. However, I haven't been successful with anything other than through the class "blue", which works but also activates something else. How do I target the "data-events" or "data-events-target"?

Is there a way to something similar to: $("click:onAddNote").trigger("click");


Solution

  • You can use querySelector and select the button with its attributes

    var button = document.querySelector('[data-events="click:onAddNote"]');
    button.onclick = ()=>console.log('Clicked');
    button.click()
    <button 
       type="button" 
       class="blue" 
       data-events="click:onAddNote" 
       data-events-target="cpoe-LabResultDetailView"
    >
       Add Note
    </button>