O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Disabling Form Elements

$("#cbSame").on("click", function(){   if($(this).is(":checked")){     $("#billInfo :input").attr("disabled",true); }   else {     $("#billInfo :input").removeAttr("disabled"); } });

Disabling web elements still displays them, but it prevents the user from interacting with them. Typically, it only makes sense to disable a form element instead of disable it if you still want the user to be able to see the values of the elements.

To disable a form element, you need to set the disabled attribute. In JavaScript, you can do this directly on the DOM object. In jQuery, you use the .attr() method. For example:

$("#deadElement").attr("disabled", "disabled");

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required