1

I would like to get all the attribute properties with a Jquery event. I know I can retrieve info of the element by doing something like this:

$(this).attr('id');

So if I want to include the class I would do something like this:

$(this).attr('id');
$(this).attr('class');

However I want to shorten/simplify the code as much as possible, so I was wondering whether I could get all the attribute properties at once without specifying them first. Is there a way to do this? Maybe a plugin?

Youss
  • 4,196
  • 12
  • 55
  • 109
  • I have taken a look at the answer provided in the duplicate, it starts out with `var el = document.getElementById("someId");` which means some info is already known to start with. This is not the case in my code, I have absolutely no info to begin with but would like to get it on click or so – Youss Apr 13 '13 at 10:45
  • @Youss, Inside a `click` handler, `this` will be bound to the element that was clicked, so you do not need `getElementById()` at all. – Frédéric Hamidi Apr 13 '13 at 10:47
  • @Frédéric Hamidi Doesn't work for me at (link) [JsFiddle](http://jsfiddle.net/yeZrC/1/) – Youss Apr 13 '13 at 10:54
  • @Youss, the code in your fiddle does not define any `click` handler. – Frédéric Hamidi Apr 13 '13 at 10:55
  • @ Frédéric Hamidi How is that important?? http://jsfiddle.net/yeZrC/2/ – Youss Apr 13 '13 at 10:57
  • What do you mean by all attributes? – Pablo Jomer Apr 13 '13 at 11:00

1 Answers1

2

Here's an example:

<div id="test" class="test" type="test"/>

// select the test element and bind the handler
$('#test').click(function(){
    // loop over it's attributes on click
    for(var i = 0;i < this.attributes.length;i++){
        console.log(this.attributes[i]);
    }
});

Demo: http://jsfiddle.net/louisbros/75w7y/

louisbros
  • 865
  • 5
  • 10