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.
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.
I'm not exactly sure what you're trying to accomplish, but you should be able to do something like this:
$('form:first').children(':first').val();
This will get the value of the first child node within the first tag in the DOM.
$("#formid input").each(function(){
alert($(this).attr("value"))
})
Well, translated literally, it'd be:
$('form:first *:nth-child(i)').val()
But jQuery makes it easy to grab elements by other manners such as ID or CSS selector. It'd be easier to maintain if you did something like:
$('form#id input.name').val()
The solution will depend on your markup.
If you have an ID on your form, you could select it by that. While you could easily select from the elements by index, there's likely a nicer way.
var $theForm = $('#idOfForm'); // Select the form with the ID "idOfForm"
// To get the value of the fourth form element.
var theValue = $theForm.find('input,select,textarea').eq(3).val();
Again, there are so many ways in jQuery to get the element you want, that there's likely something much nicer than this.
This will give you all elements under the form. Including non form elements:
$("#[form id]").find()
Then you can use an each function to traverse all the children. Or you can use the input selector to only return the form elements:
$("#[form id] :input")
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).