jQuery disable an element


If you use jQuery, you can disable an HTML element by setting the ‘disabled’ attribute to ‘disabled’ (to disable it). The result of which looks something like this:

// Disable #x
 $("#x").attr("disabled","disabled");

Here is a demo you can copy and paste into a HTML file and try it out yourself:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <select id="x" style="width:200px;">
   <option>one</option>
   <option>two</option>
  </select>
  <input type="button" value="Disable" onclick="$('#x').attr('disabled','disabled')"/>
</body>
</html>
Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Technorati
  • Twitter
  • Yahoo! Bookmarks

  1. No comments yet.
(will not be published)