0

I am trying to click a button but the only thing that defines it is multiple classes. The element I want to click is

<div class="U26fgb XHsn7e obPDgb M9Bg4d">This is a button </div>

How would I go about clicking it using Javascript?

  • Does this answer your question? [How to get elements with multiple classes](https://stackoverflow.com/questions/7184562/how-to-get-elements-with-multiple-classes) – Heretic Monkey Sep 11 '20 at 16:29
  • Just a side question @DylanLonglett are those classes static or dynamically generated? – Mihail Minkov Sep 11 '20 at 16:39

4 Answers4

3

As long as it is the only <div> element with that class combination, you'd use .querySelector(), which accepts any valid CSS selector as an argument so you can select elements in JavaScript the same way you would in CSS:

// Scan the document for the <div> that has the required classes
let theDiv = document.querySelector("div.U26fgb.XHsn7e.obPDgb.M9Bg4d");

// Set up a click event handling function
theDiv.addEventListener("click", function(){
  console.log("you clicked me");
});


// Trigger the click event of the <div>
theDiv.click();
<div class="U26fgb XHsn7e obPDgb M9Bg4d">Click Me</div>

FYI: You should get out of the habit of putting spaces on the insides of the < and > delimiters in HTML. Use this:

<div class="U26fgb XHsn7e obPDgb M9Bg4d">Click Me</div>

Not this:

< div class="U26fgb XHsn7e obPDgb M9Bg4d" >Click Me< /div >
Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
0

very simple with jQuery:

$(".U26fgb.XHsn7e.obPDgb.M9Bg4d").click(function(){
  console.log("clicked!");
}); 
Guy Louzon
  • 1,175
  • 9
  • 19
  • It's also very simple without JQuery. The question was not tagged with JQuery so you should avoid providing answers that use something the question wasn't tagged with. Also, for something this trivial, JQuery is completely unecessary. – Scott Marcus Sep 11 '20 at 17:42
-1
const div = document.querySelector('div .M9Bg4d');
div.addEventListener("click", ()=> {
 // here put what you wanna do after clicking the div.

});

-1

onclick attribute works well inside almost all the html tags and here is the simple solution to click on the div and get a result. All the Best!

function clickDiv(){
console.log("Div is Clicked");
}
<div class="U26fgb XHsn7e obPDgb M9Bg4d" onclick="clickDiv()">This is a button </div>
Ritika Gupta
  • 376
  • 1
  • 16
  • This does not address the question that was asked. – Scott Marcus Sep 11 '20 at 17:33
  • but the question says that
    This is a button
    , this entity needs have click operation linked to it, using javascript and so is stated in the answer provided. @Scott Marcus
    – Ritika Gupta Sep 11 '20 at 18:00
  • In bold, the OP says "How would I go about clicking it using Javascript?" Not, how would I go about setting up a click event. Also, it's 2020, not 1997. [Inline event attributes should not be used.](https://stackoverflow.com/questions/43459890/javascript-function-doesnt-work-when-link-is-clicked/43459991#43459991) – Scott Marcus Sep 11 '20 at 20:35
  • I may have misunderstood the context of the question posted, as I am still familiarizing myself with stackoverflow and it's users but answering the "it's 2020 and not 1997, the 'onclick' attribute in html is yet operational and not declared deprecated by w3c as of 2020, also in 2020 we do have many frameworks based on JS like AgularJS, VueJS etc. which could be used, and their attributes are too based on 'onclick' syntax only like : the angularJs's event binding methods. @Scott Marcus – Ritika Gupta Sep 14 '20 at 08:51
  • Whether something is deprecated or not is not an indication of it is a best practice. Inline event attributes are likely to never be deprecated simply because of how prevalent they are. Additionally, if you looked at the W3C link in the link I provided that explains the many reasons why inline event attributes should not be used, you'll see that they do not follow the DOM standard for event listeners.Also, you confuse framework syntax with native syntax. After you write your Angular code and it renders to the browser, what do you see? – Scott Marcus Sep 14 '20 at 12:24
  • In short, inline event attributes are an ancient way of getting something done that we used when there was no other way. 25 years later, there is not only another way, but a better way, however because of how easy inline attributes are and because of how uninformed the new JavaScript developers are, they continue to use this syntax that has very real reasons for why it should not be used. – Scott Marcus Sep 14 '20 at 12:26