Click on div please in Pure JS

how do i click on the div Click Me Please?

`

``
<script>
    var myVar = setInterval(myTimer, 10000);
    function myTimer() {

        document.querySelector(".viva-o-brazil div:nth-child(12) div:nth-child(1)  div:nth-child(16)").click(
                ); }
</script>
<div class="viva-o-brazil">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div>
        <div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div>Click Me Please</div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
            <div></div>
        </div>
        <div></div>
    </div>
    <div></div>
    <div></div>
</div>

Thanks in advance

Uhhh what is the question? I think you forgot it. :wink:

Here’s what it’s about.

...
            <div>Click Me Please</div>
...

He wants to know how to get JavaScript to trigger a click event on that specific div element.

Ok well, first of all your click event is not actually doing anything. Saying “click” isn’t telling the script what to do when it clicks. So setup an actual function to call when they click. Secondly, setInterval is designed to keep calling every X seconds. Since you are binding an event, and you only want to do it once, use setTimeout. Below is an example of how this all works…

// After 10 seconds, bind the click handler in the function 'myTimer'
  var myVar = window.setTimeout(myTimer, 10000);

// Notice we use addEventListener to attach a click handler that when the div is clicked, it calls "doStuff" function
  function myTimer() {
      document.querySelector(".viva-o-brazil div:nth-child(12) div:nth-child(1) div:nth-child(16)").addEventListener("click", doStuff);
  } 

// Do something when the element is clicked
function doStuff() {
  alert("Do stuff");
}

So with this script, you have to wait 10 seconds and then you can click your element and it will print an alert message.

1 Like

My inference, is that the page already has something on there that can normally be clicked. He’s trying to create a script to automate that click action.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.