0

I'm new in javascript and jQuery and I just read a code like $('#myForm').get(0).submit()

As far as my understanding, the myForm is an id for a <form> and it should be an unique id. So why this code use get(0) which seem the selector returns an array. Will there be more than one html elements with same id myForm ?

ricemaster
  • 25
  • 5
  • When using the Id there should only be one. Don't worry about the get(0) see http://stackoverflow.com/a/1053890/1071091 – Captain0 Apr 01 '16 at 08:06
  • 1
    **Any** jQuery selector returns array (actually array-lke object) **regardless** of result number of matches. – hindmost Apr 01 '16 at 08:06

5 Answers5

1

jquery always returns an array but you can work with it like an single object.

e.g.:

$("#someID").submit();

or any other jquery function:

$("#someID").html("<h1>test</h1>");

you also can get the javascript element with:

$("#someID")[0];
warch
  • 2,387
  • 2
  • 26
  • 43
  • jQuery selector returns always an Object. Check it with typeof. – PVL Apr 01 '16 at 08:16
  • try this :) `var x = new Array();` `typeof(x); // returns object ;)` – warch Apr 01 '16 at 08:19
  • Oh right I forgot that it's more complicated to check in javascript if something is an array since everything is actualy an object. You can check it with this if( Object.prototype.toString.call( x) === '[object Array]' ) { alert( 'Array!' ); } – PVL Apr 01 '16 at 08:23
1

For id selectors, jQuery uses the JavaScript function document.getElementById()

What it do is selects a single element with the given id attribute. Jquery returns it wrapped in custom object. From the devx blog:

Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example). More importantly, you can also apply jQuery functions against all the selected elements.

layonez
  • 1,746
  • 1
  • 16
  • 20
1

As far as my understanding, the myForm is an id for a <form> and it should be an unique id. So why this code use get(0) which seem the selector returns an array.

That is same as this:

$('#myForm')[0].submit(); 

The thing to notice is that, jQuery selector gives you a jQuery object and only jQuery methods can be applied on it.

But in your case it seems that you want to trigger the native DOM submit event and that only can be applied on dom nodes not on jQuery objects. So, .get(0) returns a DOM node and .submit() event has been triggered on it. Although that can be simplified in javascript as :

document.querySelector('#myForm').submit();

or

document.getElementById('myForm').submit();

If you get to the .get() docs:

Description: Retrieve one of the elements matched by the jQuery object. The .get() method grants access to the DOM nodes underlying each jQuery object. If the value of index is out of bounds — less than the negative number of elements or equal to or greater than the number of elements — it returns undefined.

Jai
  • 74,255
  • 12
  • 74
  • 103
1

It is jquery selector returns an array. If DOM is correct, which means id is unique for each element, then you sure can use

   $('#form_id').submit()

instead of

   $('#form_id').get(0).submit()

However, $('#form_id') returns an jquery object while $('#form_id').get(0) returns a DOM element. jquery object can use jquery functions.

  $('#id')//jquery object array
  $('#id')[0]//first element of this array
  $('#id').get(0)//first element of this array, DOM element
  $('#id').eq(0)//first element of this array, jquery object
jilykate
  • 5,430
  • 2
  • 17
  • 26
0

As you can see in the documentation: https://api.jquery.com/submit/ you can exec submit function directly to the form object:

$('#myForm').submit();

Furthermore, as you can read here https://api.jquery.com/get/

Each jQuery object also masquerades as an array

icsbcn
  • 11
  • 6