Alex Rivera | Logout

Javascript/jQuery - How do I obtain name of the class of clicked element?

Asked 2010-09-23T00:58:17.403
15

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?

Edit
Report

2 Answers

8
$(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.

answered 2010-09-23T01:05:31.893
2
$('#ele').click(function() {
    alert($(this).attr('class'));
});

And here are all of the attribute functions.

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

answered 2010-09-23T01:03:16.330

Your Answer