i have this variable
formEditable = document.getElementById("formid").getElementsByTagName("input");
i want it like that
formEditable = $("#formid").getElementsByTagName("input");
i have this variable
formEditable = document.getElementById("formid").getElementsByTagName("input");
i want it like that
formEditable = $("#formid").getElementsByTagName("input");
3
Answers
You can use a single selector string instead:
This will give you a jQuery collection of all
input
elements which are descendants of the#formid
element.If you had to use
getElementsByTagName
instead, extract the element from the$("#formid")
collection first:This will give you a live HTMLCollection of those inputs.
You Can simply use below code :
In case you have many
<input>
s in the page, you should usefor performance reasons since selectors are evaluated right-to-left.
When using the query
$("#formid input")
, first all<input>
s are located, then they are filtered based on their ancestor elements, looking for an ancestor with IDformid
.This is nothing unique to jQuery, but your original solution actually takes this into account:
Note that jQuery queries return a jQuery Object, not a
DOMNodeList
of elements. So the variableformEditable
will be a jQuery Object. To convert a jQuery Object to an array, use thetoArray
method.