Disable/Enable Form Elements
Using jQuery, you can easily disable form elements by setting the disabled attribute value of a form element to disabled. To do this, we simply select an input, and then using the attr()
method, we set the disabled attribute of the input to a value of disabled.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button" value="Click me"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { $('#button') .attr('disabled', 'disabled'); })(jQuery); </script> </body> </html>
To enable a disabled form element, we simply remove the disabled attribute using removeAttr()
or set the disabled attribute value to be empty using attr()
.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button" value="Click me" disabled="disabled"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ $('#button').removeAttr('disabled'); // or // $('#button').attr('disabled', ''); })(jQuery); </script> </body> </html>
How to Determine If a Form Element Is Disabled or Enabled
Using the jQuery form filter expressions :disabled
or :enabled,
it is rather easy to select and determine (Boolean value) if a form element is disabled or enabled. Examine the code below for clarification.
<!DOCTYPE html> <html lang="en"> <body> <input name="button" type="button" id="button1"> <input name="button" type="button" id="button2" disabled="disabled"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Is it enabled? alert($('#button1').is(':enabled')); // Alerts true // Or, using a filter alert($('#button1:enabled').length); // Alerts "1" // Is it disabled? alert($('#button2').is(':disabled')); // Alerts "true" // Or, using a filter alert($('#button2:disabled').length); // Alerts "1" })(jQuery); </script> </body> </html>
Selecting/Clearing a Single Check Box Or Radio Button
You can select a radio button input or check box by setting its checked
attribute to true
using the attr()
.
<!DOCTYPE html> <html lang="en"> <body> <input name="" value="" type="checkbox"> <input name="" value="" type="radio"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Set all check boxes or radio buttons to selected $('input:checkbox,input:radio').attr('checked', 'checked'); })(jQuery); </script> </body> </html>
To clear a radio button input or check box, simply remove the checked attribute using the removeAttr()
method or set the checked
attribute value to an empty string.
<!DOCTYPE html> <html lang="en"> <body> <input name="" type="checkbox" value="Test1" checked="checked"> <input name="" type="radio" value="Test2" checked="checked"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ $('input').removeAttr('checked'); })(jQuery); </script> </body> </html>
Selecting/Clearing Multiple Check Boxes Or Radio Button Inputs
You can use jQuery's val()
on multiple check box inputs or radio button inputs to set the inputs to checked. This is done by passing the val()
method an array containing a string that coincides with the check box input or radio button input value attribute.
<!DOCTYPE html> <html lang="en"> <body> <input type="radio" value="radio1"> <input type="radio" value="radio2"> <input type="checkbox" value="checkbox1"> <input type="checkbox" value="checkbox2"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Check all radio and check box inputs on the page. $('input:radio,input:checkbox').val(['radio1', 'radio2', 'checkbox1', 'checkbox2']); // Use explicit iteration to clear. // $('input:radio,input:checkbox').removeAttr('checked'); // or // $('input:radio,input:checkbox').attr('checked', ''); })(jQuery); </script> </body> </html>
Notes: If the check box or radio button is already selected, using val()
will not clear the input element.
Determining If a Check Box Or Radio Button Is Selected Or Cleared
We can determine if a check box input or radio button input is selected or cleared by using the :checked
form filter. Examine the code below for several usages of the :checked
filter.
<!DOCTYPE html> <html lang="en"> <body> <input checked="checked" type="checkbox"> <input checked="checked" type="radio"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Alerts "true" alert($('input:checkbox').is(':checked')); // Or, added to wrapper set if checked. Alerts "1" alert($('input:checkbox:checked').length); // Alerts "true" alert($('input:radio').is(':checked')); // Or, added to wrapper set if checked. Alerts "1" alert($('input:radio:checked').length); })(jQuery); </script> </body> </html>
How to Determine If a Form Element Is Hidden
You can determine if a form element is hidden using the :hidden
form filter. Examine the code below for several usages of the :checked
filter.
<!DOCTYPE html> <html lang="en"> <body> <input type="hidden"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Alerts "true" alert($('input').is(':hidden')); // Or, added to wrapper set if hidden. Alerts "1" alert($('input:hidden').length); })(jQuery); </script> </body> </html>
Setting/Getting the Value of an Input Element
The val()
method can be used to set and get the attribute value of an input element (button, checkbox, hidden, image, password, radio, reset, submit, text). Below, I set the value for each input in val()
and then alert the value using the val()
method.
<!DOCTYPE html> <html lang="en"> <body> <input type="button"> <input type="checkbox"> <input type="hidden"> <input type="image"> <input type="password"> <input type="radio"> <input type="reset"> <input type="submit"> <input type="text"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { $('input:button').val('I am a button'); $('input:checkbox').val('I am a check box'); $('input:hidden').val('I am a hidden input'); $('input:image').val('I am an image'); $('input:password').val('I am a password'); $('input:radio').val('I am a radio'); $('input:reset').val('I am a reset'); $('input:submit').val('I am a submit'); $('input:text').val('I am a text'); // Alerts input's value attribute alert($('input:button').val()); alert($('input:checkbox').val()); alert($('input:hidden').val()); alert($('input:image').val()); alert($('input:password').val()); alert($('input:radio').val()); alert($('input:reset').val()); alert($('input:submit').val()); alert($('input:text').val()); })(jQuery); </script> </body> </html>
Setting/Getting the Selected Option of a Select Element
Using the val()
method, you can set the selected value of a <select>
element by passing the val()
method a string representing the value assigned to the <option>
element.
To get the value of the <select>
element, use the val()
method again to determine which option is selected. The val()
method in this scenario will return the selected option's attribute value.
<!DOCTYPE html> <html lang="en"> <body> <select id="s" name="s"> <option value="option1">option one</option> <option value="option2">option two</option> </select> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the selected option in the select element to "option two" $('select').val('option2'); // Alerts "option2" alert($('select').val()); })(jQuery); </script> </body> </html>
Setting/Getting Selected Options of a Multi-Select Element
Using the val()
method we can set the selected values of a multi-select element by passing the val()
method an array containing the corresponding values.
To get the selected options in a multi-select element, we again use the val()
method to retrieve an array of the options that are selected. The array will contain the value attributes of the selected options.
<!DOCTYPE html> <html lang="en"> <body> <select size="4" multiple="multiple"> <option value="option1">option one</option> <option value="option2">option two</option> <option value="option3">option three</option> <option value="option4">option four</option> </select> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Set the value of the selected options $('select').val(['option2', 'option4']); // Get the selected values alert($('select').val().join(', ')); // Alerts, "option2, option4" })(jQuery); </script> </body> </html>
Setting/Getting Text Contained Within a <textarea>
You can set the text node contents of a <textarea>
element by passing the val()
method a text string to be used as the text. To get the value of a <textarea>
element, we again use the val()
method to retrieve the text contained within.
<!DOCTYPE html> <html lang="en"> <body> <textarea></textarea> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the text contained within $('textarea').val('I am a textarea'); // Alerts "I am a textarea" alert($('textarea').val()); })(jQuery); </script> </body> </html>
Setting/Getting the Value Attribute of a Button Element
You can set the value attribute of a button element by passing the val()
method a text string. To get the value of a button element, use the val()
method again to retrieve the text.
<!DOCTYPE html> <html lang="en"> <body> <button>Button</button> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function ($) { // Set the value: <button value="I am a Button Element"> $('button').val('I am a Button Element') // Alerts "I am a Button Element" alert($('button').val()); })(jQuery); </script> </body> </html>
Editing Select Elements
jQuery makes some of the common tasks associated with editing select elements trivial. Below are some of those tasks with coded examples.
// Add options to a select element at the end $('select').append('<option value="">option</option>'); // Add options to the start of a select element $('select').prepend('<option value="">option</option>'); // Replace all the options with new options $('select').html('<option value="">option</option><option value="">option</option>'); // Replace items at a certain index using the :eq() selecting filter to // select the element, and then replace it with the .replaceWith() method $('select option:eq(1)').replaceWith('<option value="">option</option>'); // Set the select elements' selected option to index 2 $('select option:eq(2)').attr('selected', 'selected'); // Remove the last option from a select element $('select option:last').remove(); // Select an option from a select element via its // order in the wrapper set using custom filters $('#select option:first'); $('#select option:last'); $('#select option:eq(3)'); $('#select option:gt(5)'); $('#select option:lt(3)'); $('#select option:not(:selected)'); // Get the text of the selected option(s), this will return the text of // all options that are selected when dealing with a multi-select element $('select option:selected').text(); // Get the value attribute value of an option in a select element $('select option:last').val(); // Getting the :last option element // Get the index (0 index) of the selected option. // Note: Does not work with multi-select elements. $('select option').index($('select option:selected')); // Insert an option after a particular position $('select option:eq(1)').after('<option value="">option</option>'); // Insert an option before a particular position $('select option:eq(3)').before('<option value="">option</option>');
Selecting Form Elements By Type
It is possible to select form elements by their type-e.g. $('input:checkbox')
. jQuery provides the following form type filters for selecting form elements by their type.
:text
:password
:radio
:checkbox
:submit
:image
:reset
:file
:button
Selecting All Form Elements
You can select all form elements by using the :input
form filter. This filter will select more than just input elements, it will select any <textarea>
, <select>
, or <button>
elements as well. In the coded example below, take notice of the length of the wrapper set when using the :input
filter.
<!DOCTYPE html> <html lang="en"> <body> <input type="button" value="Input Button"> <input type="checkbox"> <input type="file"> <input type="hidden"> <input type="image"> <input type="password"> <input type="radio"> <input type="reset"> <input type="submit"> <input type="text"> <select> <option>Option</option> </select> <textarea></textarea> <button>Button</button> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script> <script> (function($){ // Alerts "13" form elements alert($(':input').length); })(jQuery); </script> </body> </html>
Comments