Explosion Pills
Explosion Pills

Reputation: 191749

accessing element attributes with jquery vs. plain javascript, which is faster?

Which is faster: $("#element")[0].value or $("#element").val()? If the former is faster, what is the purpose of the latter?

Upvotes: 4

Views: 272

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195992

the same as $("#element") being slower than document.getElementById('element');

ease of use, consistency in the framework, hiding of cross-browser implementation (inconsistencies, not in the particular example but that is the concept of frameworks)..

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630429

$("#element")[0].value is faster, native code is always faster.

Even faster would be document.getElementById("element").value.

The .val() function is to work for all input types, including <textarea> and <select> elements. Underneath, for everything that's not an <option> or a <select> or a <input type="radio"> (in some cases) it gets the .value.

Upvotes: 10

Related Questions