0

I have this HTML element

<div dir="auto" data-tab="1" contenteditable="true" class="input" data-reactid=".0.$main.5.2.1.1.0.1"></div>

and want to access it from JavaScript code then send it mouse click event and then keyboard key-press events .

I have searching a lot and find this question Is there a way to simulate key presses or a click with javascript? but I couldn't access the element ! Can anyone help me to find a way to access it from javascript and send it mouse click event and then keyboard key-press events ?

Community
  • 1
  • 1
Maadh
  • 643
  • 4
  • 24
  • You''re not going to want to send it a mouse click event, but a focus event. Try `document.getElementById('myID').focus();` and then fire keyboard events – winhowes Feb 01 '16 at 02:42
  • Thank you @winhowes But there is no Id for this div could you help me to access it please ? – Maadh Feb 01 '16 at 02:44
  • Take a look at https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector – winhowes Feb 01 '16 at 02:46

1 Answers1

1

To access your particular element I could use a querySelector with an attribute that looks unique to me (I'm not sure if that's your reality):

var myElement = document.querySelector('[data-reactid=".0.$main.5.2.1.1.0.1"]')

Then, to set a focus:

myElement.focus();

To send a click:

myElement.click();

Or, to change a background color:

myElement.setAttribute("style", "background-color: red;");
Alex R.
  • 644
  • 6
  • 9