0

I am trying to click a div with Javascript. I can use Jquery. After I select the element using QuerySelector or XPath and use .click() on the element, it doesn't actually end up clicking the div and does nothing.

document.querySelector('.GriddyLayout > div:nth-child(1)').click()

The website I am trying it on is here. Running the code above in the Javascript console is not selecting the option for some reason. Does anyone know how to click this button (div) with JS so that it actually clicks? Will upvote all answers, and accept the one that works best/first.

7
  • Did you try jquery way? $('.GriddyLayout > div:nth-child(1)').click(); Commented Dec 10, 2017 at 7:13
  • I tried it, but it didn't work. Commented Dec 10, 2017 at 7:13
  • Which div are you trying to select? Commented Dec 10, 2017 at 7:20
  • <div class="SelectableTile TEXT MULTIPLE_CHOICE natural" role="radio" aria-checked="false" style="width: 90px; height: 50px;" id="yui_3_14_0_1_1512889344567_1009"><div class="TileSkinBare CENTER MIDDLE"><div class="GeneticallyModified"><div id="yui_3_14_0_1_1512889344567_868">28</div></div></div></div> Commented Dec 10, 2017 at 7:21
  • What about just: document.querySelector('.GriddyLayout > div')? Your selector seems redundant. Commented Dec 10, 2017 at 7:22

4 Answers 4

4

You need to trigger some events in specific sequence. In javascript, you could use:

var targetNode = document.querySelector('.GriddyLayout > div:nth-child(1)');

triggerMouseEvent(targetNode, "mousedown");
triggerMouseEvent(targetNode, "mouseup");
triggerMouseEvent(targetNode, "click");

function triggerMouseEvent(node, eventType) {
  var clickEvent = document.createEvent('MouseEvents');
  clickEvent.initEvent(eventType, true, true);
  node.dispatchEvent(clickEvent);
}
Sign up to request clarification or add additional context in comments.

2 Comments

@A. Wolff How did you figure out that it requires all mousedown, up and then click?
@Miro By inspecting, using chrome's inspector, the fired events on element's click. This is btw a classic way to avoid some bot's click. Google is using this kind of trick too BUT very more complex
1

You can achieve this by using plain JavaScript.

var divs = document.getElementsByClassName("GriddyLayout");
 for(var i = 0; i < divs.length; i++){
  divs[i].addEventListener('click', function() {
  console.log('clicked' +"  "+this.innerHTML);
});
}

function callClickEvents() {
for(var i = 0; i < divs.length; i++){
  divs[i].click();
}
}

setTimeout(function(){ callClickEvents() }, 100);

// click nth element i.e 3rd (2+1)
divs[2].click(); 
console.log("Now click all divs");
<div class="GriddyLayout">hello 1</div>
<div class="GriddyLayout">hello 2</div>
<div class="GriddyLayout">hello 3</div>
<div class="GriddyLayout">hello 4</div>
                 .
<div class="GriddyLayout">hello n</div>

6 Comments

That is the wrong element. It doesn't refer to the button, it refers to the group of buttons
Maybe: document.getElementsByClassName('GriddyLayout')[0].firstElementChild.click();
document.getElementsByClassName('GriddyLayout')[0] is not the right element. I already tried the parents and children
What's weird that you don't have jquery but sizzle works. For example $('.GriddyLayout > div:nth-child(1)').className += " selected"; works. But there is not click() function.
@manikant check the website he linked. He's trying to "click" an answer without jQuery.
|
1

enter image description heretry this:document.querySelector('.GriddyLayout > div:nth-child(1)').addEventListener('click', function() { console.log('clicked') })

5 Comments

I tried on your site and it is working. Based on your code, it means that only the first option when clicked will log 'clicked'
Check the screenshot attached, It's strange that it is not working for you. When are you attaching the eventlistener?
Huh... That is weird. It doesn't work for me... It doesn't even say clicked
Make sure you are not attaching the eventlistener before the element is present in the DOM
But it would error if that happened because the element would not be visible. However, it did not error in my case, which means that it is a different problem
1

Change selector to .GriddyLayout > div.SelectableTile:nth-child(1). You aren't using jQuery.

With javascript

document.querySelector('.GriddyLayout > div.SelectableTile:nth-child(1)').addEventListener('click', function() {console.log('clicked');} );

With jQuery

jQuery('.GriddyLayout > div.SelectableTile:nth-child(1)').click(function() { console.log('clicked');});

1 Comment

This does not click the div, it justs adds an event listener for when the div is clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.