jQuery Succinctly: HTML Forms & jQuery

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.

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().


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.


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().

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.


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.

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.


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.


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.


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.


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.


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.


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.


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.


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.

Tags:

Comments

Related Articles