Tuesday, March 27, 2012
Get all input values by jQuery's serialize function
How to get and set form element values with jQuery
How to get and set form element values with jQuery
Posted November 13th, 2008 in Javascript (Updated April 22nd, 2010)
jQuery is a Javascript framework which can simplify coding Javascript for a website and removes a lot of cross browser compatibility issues. This post looks at how to get and set the values of HTML form elements with jQuery.
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:
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:
1 | window.alert( $( '#foo' ).val() ); |
2 | window.alert( $( '#bar' ).val() ); |
If we're using the name only and not specifying an id, the jQuery to get the form values would be this:
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:
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:
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:
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:
1 | $( 'input[name="baz"]' )[0].checked = true ; |
[0] would set the first one checked, [1] would 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, April 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)
Wednesday, March 21, 2012
Finding Max And Min Values with Versioned Documents
Monday, March 12, 2012
GettingStarted - Cassandra Wiki
Press "Control-C" to stop Cassandra.
If you start up Cassandra without "-f" option, it will run in background, so you need to kill the process to stop.
setup passphraseless ssh
Sunday, March 11, 2012
Welcome to Apache™ Hadoop™!
The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using a simple programming model. It is designed to scale up from single servers to thousands of machines, each offering local computation and storage. Rather than rely on hardware to deliver high-avaiability, the library itself is designed to detect and handle failures at the application layer, so delivering a highly-availabile service on top of a cluster of computers, each of which may be prone to failures.
Where Wizards Fear To Tread - Perl.com
However, for us module authors who already have to support our modules on different versions of perl and different platforms, we now have to deal with another case: threads! This article will show you how threads relate to modules, how we can take old modules and make them thread-safe, and round off with a new module that alters perl's behavior of the "current working directory".
To run the examples I have shown here, you need perl 5.8 RC1 or later compiled with threads. On Unix, you can use Configure -Duseithreads -Dusethreads; On Win32, the default build will always have threading enabled.
Saturday, March 10, 2012
SQL Server 2012 Virtual Launch Event
Immerse yourself in the exciting New World of Data with SQL Server 2012. Engage not only Microsoft product teams, but with our partners and our most ground breaking customers. SQL Server 2012 is part of the broadest Data Platforms in technology today. Discover how it enables mission critical confidence, blazing fast performance, and stunning interactive data visualizations.
Simple Helix | Hosting Solutions
When your business is e-commerce, your website is critical, so it's important to build a site that is reliable and secure.
The Simple Helix platform is a high performance hosting environment that is built and optimized specifically for Magento to deliver that reliability and security, with the fastest page-loading speed for your website!
Dev-Team Blog - Welcome new A5 jailbreakers!
Here’s a quick breakdown of how many A5 owners have jailbroken their devices since Friday morning. The numbers as of Monday afternoon are:
491,325 new iPhone4,1 devices
308,967 new iPad2 devices
152,940 previously jailbroken (at 4.x) iPad2 devices
Total: 953,232 new A5 jailbreaks in a little over 3 days
The reason these numbers can be so precise is that one of the housekeeping activities that happens when you launch Cydia is a query to @saurik’s server for the list of available SHSH blobs. (Even if you have none on file, the query is still made).
Welcome to the jailbreak family!
P.S. Remember the cardinal rule of jailbreaking: never update your firmware until a new jailbreak is available. This is especially true for A5 owners, who currently have no way of restoring to 5.0.1 once the 5.0.1 SHSH blob signing window is closed.
Comments
Glad you liked it. Would you like to share?
Add New Comment
Showing 19 comments