Vicky
Vicky

Reputation: 9575

How to get form specific variable value using Jquery

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

Upvotes: 3

Views: 13785

Answers (4)

user1928896
user1928896

Reputation: 673

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

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

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89209

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

Upvotes: 1

cletus
cletus

Reputation: 625485

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.

Upvotes: 8

RageZ
RageZ

Reputation: 27323

what about

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

you can find more about CSS2 selector here and here

Upvotes: 2

Related Questions