Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the jQuery equivalent to document.forms[0].elements[i].value;?

What is the jquery equivalent to: document.forms[0].elements[i].value;?

I don't know how to travel through a form and its elements in jQuery and would like to know how to do it.

like image 281
Ian Avatar asked Jul 19 '10 22:07

Ian


People also ask

What is document forms 0 submit ()?

forms[0]. submit will trigger the submission of the first form in your HTML page. Indeed, documents. forms contains all the forms of your document. You can access them with their name attribute or their index.

What is document forms in JavaScript?

documents. forms is an object containing all of the forms for that HTML document. With this code, you are referencing the elements by their name attributes (not id ). So this would provide a string containing the value for the form element with the name "email" within the form with the name "myForm".

How do you reference a form in JavaScript?

In order to access a form through JavaScript, we need to obtain a reference to the form object. One obvious way to approach, is to use the getElementById method. For instance, if we had a form with the id attribute "subscribe_frm" , we could access the form in this way: var oForm = document.


1 Answers

The usual translation is the :input selector:

$("form:first :input").each(function() {
  alert($(this).val()); //alerts the value
});

The :first is because your example pulls the first <form>, if there's only one or you want all input elements, just take the :first off. The :input selector works for <input>, <select>, <textarea>...all the elements you typically care about here.

However, if we knew exactly what your goal is, there's probably a very simple way to achieve it. If you can post more info, like the HTML and what values you want to extract (or do something else with).

like image 136
Nick Craver Avatar answered Sep 22 '22 05:09

Nick Craver