tags:

views:

62

answers:

3

Hi,

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

+2  A: 

what about

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

you can find more about CSS2 selector here and here

RageZ
+2  A: 

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.

cletus
+1  A: 

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

The Elite Gentleman