views:

52

answers:

5

I googled and googled and I concluded that it's very hard to get answer on my own.

I am trying to use jquery or JavaScript to get a property of clicked element. I can use "this.hash" for example - it returns hash value I presume.

Now I would like to get name of the class of clicked element. Is it even possible? How? And where would I find this kind of information?

  • jQuery documentation? - All I can find is methods and plugins, no properties.. if its there - please provide me with link.

  • JavaScript documentation? - is there even one comprehensive one? again please a link.

  • DOM documentation? - the one on W3C or where (link appreciated).

And what is this.hash? - DOM JavaScript or jQuery?

+2  A: 

In jQuery, if you attach a click event to all <div> tags (for example), you can get it's class like this:

Example: http://jsfiddle.net/wpNST/

$('div').click(function() {
    var theClass = this.className;  // "this" is the element clicked
    alert( theClass );
});

This uses jQuery's .click(fn) method to assign the handler, but access the className property directly from the DOM element that was clicked, which is represented by this.

There are jQuery methods that do this as well, like .attr().

Example: http://jsfiddle.net/wpNST/1/

$('div').click(function() {
    var theClass = $(this).attr('class');
    alert( theClass );
});

Here I wrapped the DOM element with a jQuery object so that it can use the methods made available by jQuery. The .attr() method here gets the class that was set.

patrick dw
+1  A: 
$('#ele').click(function() {
    alert($(this).attr('class'));
});

And here are all of the attribute functions.

http://api.jquery.com/category/attributes/

Metropolis
+2  A: 

This example will work on every element in the page. I'd recommend using console.log(event) and poking around at what it dumps into your console with Firebug/Developer tools.

jQuery

​$(window).click(function(e) {
    console.log(e); // then e.srcElement.className has the class
});​​​​

Javascript

window.onclick = function(e) {
    console.log(e); // then e.srcElement.className has the class
}​

Try it out

http://jsfiddle.net/M2Wvp/

Edit
For clarification, you don't have to log console for the e.srcElement.className to have the class, hopefully that doesn't confuse anyone. It's meant to show that within the function, that will have the class name.

Robert
+1  A: 

You can use element.className.split(/\s+/); to get you an array of class names, remember elements can have more than one class.

Then you can iterate all of them and find the one you want.

window.onclick = function(e) {
    var classList = e.srcElement.className.split(/\s+/);
    for (i = 0; i < classList.length; i++) {
       if (classList[i] === 'someClass') {
         //do something
       }
    }
}

jQuery does not really help you here but if you must

$(document).click(function(){
    var classList =$(this).attr('class').split(/\s+/);
    $.each( classList, function(index, item){
        if (item==='someClass') {
           //do something
        }
    });
});
redsquare
A: 
$(document).click(function(e){
    var clickElement = e.target;  // get the dom element clicked.
    var elementClassName = e.target.className;  // get the classname of the element clicked
});

this supports on clicking anywhere of the page. if the element you clicked doesn't have a class name, it will return null or empty string.

rob waminal