Get started with jquery

Source: Internet
Author: User
Getting a form value with jquery

To get a form value with jquery use the Val () function. Let's say we have a form like this, using an ID for each form element:

View sourceprint?
1 <input name="foo" id="foo" type="text">
2   
3 <select name="foo" id="bar">
4     <option value="1">one</option>
5     <option value="2">two</option>
6     <option value="3">three</option>
7 </select>

We can display an alert for the values of "foo" and "bar" as easily this:

View sourceprint?
1 window.alert( $('#foo').val() );
2 window.alert( $('#bar').val() );

If we're re using the name only and not specifying an ID, The jquery to get the form values wocould be this:

View sourceprint?
1 window.alert( $('[name=foo]').val() );
2 window.alert( $('[name=bar]').val() );

If you have a group of radio buttons and want to get the selected button, the code is slightly different because they all have the same name. using the above code examples will show the value for the first radio button on the form with that name. to find out the value of the checked one, do this instead:

HTML:

View sourceprint?
1 <input type="radio" name="baz" value="x">
2 <input type="radio" name="baz" value="y">
3 <input type="radio" name="baz" value="z">

Jquery:

View sourceprint?
1 window.alert($('input[name=baz]:checked').val());
Setting a form value with jquery

You can set the form values with jquery using the same Val () function but passing it a new value instead. using the same example forms above, you 'd do this for the text input and select box:

View sourceprint?
1 $('#foo').val('this is some example text');
2 $('#bar').val('3');
3 OR
4 $('[name=foo]').val('this is some example text');
5 $('[name=bar]').val('3');

Using the above for a radio button will change the actual value of the radio button rather than changing the one that is selected. To change the radio button that is selected you 'd do this:

View sourceprint?
1 $('input[name="baz"]')[0].checked = true;

[0] wocould set the first one checked, [1] wocould set the second one checked and so on.

Working demos

For working demos of the above examples (and more) please see my demos for getting and setting form values with jquery, how to check and uncheck a checkbox with jquery, and clear a form with jquery posts.

Related posts:
  • Demos for getting and setting form values with jquery (Thursday, rjl 22nd 2010)
  • Clear a form with jquery (Friday, June 19th 2009)
  • Dynamically get and set an elements content with jquery (Sunday, January 11th 2009)
  • Adding, removing and checking if an element has a CSS class with jquery (Tuesday, December 16th 2008)
  • How to check and uncheck a checkbox with jquery (Friday, November 14th 2008)
  • Clearing the default value of text input with JavaScript (Tuesday, June 17th 2008)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.