11.06.2018 Views

jQuery Succinctly

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5 HTML Forms<br />

Disable/enable form elements<br />

Using <strong>jQuery</strong>, you can easily disable form elements by setting the disabled attribute value of a<br />

form element to disabled. To do this, we simply select an input, and then using the attr()<br />

method, we set the disabled attribute of the input to a value of disabled.<br />

Sample: sample51.html<br />

<br />

<br />

<br />

<br />

<br />

(function ($) {<br />

$('#button')<br />

.attr('disabled', 'disabled');<br />

})(<strong>jQuery</strong>); <br />

<br />

<br />

To enable a disabled form element, we simply remove the disabled attribute using<br />

removeAttr() or set the disabled attribute value to be empty using attr().<br />

Sample: sample52.html<br />

<br />

<br />

<br />

<br />

<br />

(function($){ $('#button').removeAttr('disabled');<br />

// or<br />

// $('#button').attr('disabled', '');<br />

})(<strong>jQuery</strong>); <br />

<br />

<br />

How to determine if a form element is disabled or enabled<br />

Using the <strong>jQuery</strong> form filter expressions :disabled or :enabled, it is rather easy to select and<br />

determine (Boolean value) if a form element is disabled or enabled. Examine the code below for<br />

clarification.<br />

54

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!