-3

Here is my code:

doc.on("click", ".add_your_qora", function(e){
     // in here I need to know, has this element also "myclass" class too?
})

As I've commented in the code, I need to know, has .add_your_qora element also myclass or not? How can I determine that?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • API documentation http://api.jquery.com/?s=class –  May 11 '18 at 20:12
  • Google: https://www.google.com/search?q=jquery+check+if+element+has+class –  May 11 '18 at 20:12
  • You've even used `.hasClass()` [in your own code examples](https://stackoverflow.com/questions/39661525/how-can-i-define-the-rest-of-width) –  May 11 '18 at 20:16
  • you just can define a different function for the case: `doc.on("click", ".add_your_qora.myclass", function(e){...`. It will run for `.add_your_qora` element that has `.myclass` as well. for an opposite case you can use selector `".add_your_qora:not(.myClass)` – Banzay May 11 '18 at 20:44

3 Answers3

0

You can use Element.classList.contains.

Example without jQuery:

document.querySelector(".add_your_qora").addEventListener("click", e => {
        if (e.target.classList.contains("myclass") {
            console.log("The element has myclass");
        }
};
haugsand
  • 400
  • 1
  • 5
-1

You can use $(this).hasClass('myClass');

Grant Thomas
  • 44,454
  • 10
  • 85
  • 129
-1

You can use className property, like

myFunction(){
var target = document.getElementById('the_div_id');
if(target.className === "myclass"){
    alert("it has myclass");
}
}

You can also have a look at this https://gist.github.com/sonnyt/8585696 , see if this helps you.

Karan Tewari
  • 498
  • 8
  • 20