3

How to get form specific variable value using Jquery.

I have multiple form in same page and same variable name in each form, i want to read textfield value using form name.

Please help

thanks

4 Answers 4

8

To find a set of inputs with a specific name:

$(":input[name='" + name + "'")...

You need some way to identify the form if the same name is used in different forms. For example:

<form id="one">
  <input type="text" name="txt">
</form>
<form id="two">
  <input type="text" name="txt">
</form>

would be selected with:

$("#one :input[name='txt']")...

Generally speaking it's a bad idea to use attribute selectors. A good habit to get into is giving all your form fields unique IDs so you can do this:

$("#fieldId")...

or if there are multiple, use a class:

$(":input.fieldclass")...

The val() method is used to query or set the value of a form field.

Sign up to request clarification or add additional context in comments.

Comments

2

what about

$('form[name=foobar] #yourfieldid')

you can find more about CSS2 selector here and here

Comments

1

You can use simple javascript

var formField = document.forms[form_index].field;

or

var formField = document.formName.field;

or

var formField = document.forms["formName"].field;

or JQuery

var $formField = $('form[name="formName"] > input[name="fieldName"]');

Updated my JQuery statement. It only takes every field within the form with that name fieldName

Comments

0

I know many years passed since the question. However, here is a simple selector:

var $field = $('#formId #fieldId'); var value = $field.val();

Comments

Your Answer

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.