7

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.

ThinkingStiff
65.4k31 gold badges148 silver badges241 bronze badges
asked Jul 19, 2010 at 22:34
3
  • 1
    Can you post an example of your markup, and which values you want to pull out? Commented Jul 19, 2010 at 22:38
  • Read about how to select stuff here: api.jquery.com/category/selectors and about how to traverse here: api.jquery.com/category/traversing. Commented Jul 19, 2010 at 22:50
  • I wrote a basic form validation script in javascript, and I'm trying to transition it over to JQUERY. It's a for loop that searches through all of the elements in the from checking to make sure that all the values have changed form the default (ie. first name last name) to something real. The code snippet I posted is attached to a variable that saves the current input's value and checks it against the list of default values. thisVal = document.forms[0].elements[i].value; Commented Jul 20, 2010 at 5:17

5 Answers 5

15

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).

answered Jul 19, 2010 at 22:42
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I wrote a basic form validation script in javascript, and I'm trying to transition it over to JQUERY. It's a for loop that searches through all of the elements in the form checking to make sure that all the values have changed from the default (ie. first name last name) to something real. The code snippet I posted is attached to a variable that saves the current input's value and checks it against the list of default values. thisVal = document.forms[0].elements[i].value;
I also do have 2 forms on my page. My current working version has <form action="..." onsubmit='validate("formName");' ..> to validate that specific form for thisVal = document.forms[formName].elements[i].value;
1

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()
answered Jul 19, 2010 at 22:39

Comments

0
$("#formid input").each(function(){
 alert($(this).attr("value"))
}) 
answered Jul 19, 2010 at 22:39

1 Comment

This would just grab input fields, not select or textarea.
0

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")
answered Jul 19, 2010 at 22:39

2 Comments

Form controls usually aren't children of the form
@David You're right. They tend to be nested, though not always. I guess I should have used find instead or just list the type of controls that we want to return. I will at least change to use find instead.
0

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 <form> tag in the DOM.

Robert
8,7672 gold badges29 silver badges34 bronze badges
answered Jul 19, 2010 at 22:38

3 Comments

This would return nothing...you're selecting a <form> tag :)
This is still incorrect, not my downvote...but I don't think your thinking of the selectors correctly here, this would return the first element, the example is an elements[i], which translates pretty closely to: $('form:first :input').eq(i).val();
Yup, @Nick, right again. I guess I was just thinking that he was trying to get the first child. Thanks!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.