1

i have this variable

formEditable = document.getElementById("formid").getElementsByTagName("input");

i want it like that

formEditable = $("#formid").getElementsByTagName("input");
Rtronic
  • 635
  • 5
  • 6
  • Does this answer your question? [jQuery: Get selected element tag name](https://stackoverflow.com/questions/5347357/jquery-get-selected-element-tag-name) – freetzyy Jan 29 '20 at 07:01

3 Answers3

2

In case you have many <input>s in the page, you should use

// Locate the first element with ID `formid`,
// then query its descendants for `<input>` elements.
formEditable = $("#formid").find("input");

for 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 ID formid.

This is nothing unique to jQuery, but your original solution actually takes this into account:

formEditable = document.getElementById("formid").getElementsByTagName("input");

Note that jQuery queries return a jQuery Object, not a DOMNodeList of elements. So the variable formEditable will be a jQuery Object. To convert a jQuery Object to an array, use the toArray method.

Lars Gyrup Brink Nielsen
  • 3,939
  • 2
  • 34
  • 35
1

You can use a single selector string instead:

const inputsInsideForm = $('#formid input');

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:

$("#formid")[0].getElementsByTagName("input");

This will give you a live HTMLCollection of those inputs.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • But **ID** is unique and `$("#formid")[0]` does what? Takes first ID from one ID? If `getElementsByTagName("input")[0]` - yes, it's Ok, cause it returns an array. – Aksen P Jan 29 '20 at 07:06
  • Yes, IDs are (or should be) unique, but `$(selector)` will *always* return a jQuery collection, not the underlying element. In this case, the jQuery collection will contain a single value (but, in general, jQuery collections can contain more, or zero, values). Regardless of the number of elements in the collection, `[0]`, `[1]`, etc is one of the ways to extract an underlying element from the collection - just like `querySelectorAll` – CertainPerformance Jan 29 '20 at 07:08
  • I wanna say `$("#formid")[0]` useless move. `$("#formid")` always returns the first one ID. BTW [it raises an error](https://jsfiddle.net/ba3fw0Lh/). – Aksen P Jan 29 '20 at 07:10
  • Yeah, I think it's pretty silly too - I'd always prefer `$('#formid input');` by far. Problem is that you can't use `getElementsByTagName` on a jQuery collection, and OP was trying to use `getElementsByTagName` (maybe part of a homework requirement?) so I included that to show how it could be used in combination with the jQuery method of selecting elements, even though it seems pretty silly – CertainPerformance Jan 29 '20 at 07:20
  • Yes, using a jQuery method on something which is not a jQuery collection will indeed throw an error. (`$("#s")[0]` is, of course, not a jQuery collection, it's a single element. not sure what you were expecting there. * extract the element from the $("#formid") collection first:* in the answer meant that one will have the *element*, not a jQuery collection) – CertainPerformance Jan 29 '20 at 07:21
-2

You Can simply use below code :

$("#formid").getElementsByTagName("input");
Harshsetia
  • 256
  • 2
  • 6