Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to get elements attributes? [closed]

I'm like most of novice web developers in most cases use jQuery, but more often I started to use clean js. So from here is my question: is it god practice to use clean js in jQuery scope, for example if i need to get elements class i can do this like:

jQuery('div#grid a').click(function(event){
    event.preventDefault();
    console.log(this.getAttribute('class'));
    console.log(this.className);
    console.log(jQuery(this).attr('class'));
});

But what is the best way?

like image 453
Kin Avatar asked May 06 '13 10:05

Kin


People also ask

How do I get attribute closure?

The algorithm is as follows: Add the attributes contained in the attribute set X to the result set X+. Add the attributes to the result set X+ which can be functionally determined from the attributes already contained in the result set. Repeat step 2 until no more attributes can be added to the result set X+.

Can closing tags have attributes?

Some few examples of self-closing tags in HTML are <input/>, <hr/>, <br/>, <img/>, etc. Self-closing tags in HTML only have attributes and do not contain any content, and they also cannot have any children.

Can HTML attributes be added to the closing tag of an element?

No. Because end tags are solely to define scope, the properties and attributes of the HTML element must be loaded when the tag is opened.


2 Answers

It's better to use the this.className because it is the fastest among the three. Here's a demo which you can run and see the speed of execution: jquery attr(class) vs js.className

You can see after running the test that className property is the fastest one.

like image 106
palaѕн Avatar answered Oct 09 '22 20:10

palaѕн


Yes,use clean js in jQuery scope is good practise.Because this is native work with DOM and it works more faster.

Perfomance Test

like image 38
vborutenko Avatar answered Oct 09 '22 18:10

vborutenko