Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 747
Explosion Pills Avatar asked Dec 21 '22 21:12

Explosion Pills


1 Answers

$("#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.

like image 154
Nick Craver Avatar answered Apr 25 '23 19:04

Nick Craver