jQuery : Use the :selected and :checked selectors to get at values, For selects and checkboxes

// Get the value from a dropdown select
$( "select.foo option:selected").val();
// Get the value from a dropdown select even easier
$( "select.foo" ).val();
// Get the value from a checked checkbox
$( "input:checkbox:checked" ).val();
// Get the value from a set of radio buttons
$( "input:radio[name=bar]:checked" ).val();
Note: At present, using .val() on textarea elements strips carriage return characters from the browser-reported value. When this value is sent to the server via XHR however, carriage returns are preserved (or added by browsers which do not include them in the raw value). A workaround for this issue can be achieved using a valHook as follows:
$.valHooks.textarea = {
get: function( elem ) {
return elem.value.replace( /\r?\n/g, “\r\n” );
}
};

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s