views:

35

answers:

2
 $("#btn").attr("disabled", "disabled");

i have used the above line to disable the button how would i enable it .this is not working

 $("#btn").removeAttr("disabled");
A: 

The jQuery attr-selector actually has an overload for this, where you can pass true/false for disabled specifically:

$('#btn').attr('disabled', true);
$('#btn').attr('disabled', false);
David Hedlund
This adds `disabled=""` instead of `disabled="disabled"` attribute which is the standard way.
Darin Dimitrov
@Darin Dimitrov: I'd argue that once the browser has made its internal representation of the original HTML source, you really can't make a distinction like that. don't think of it as adding an attribute, think of it as disabling an entity. what you're seeing is how this new disabled state is deserialized (if you will) into HTML by some inspecting device. consider http://http://jsbin.com/acebi3/in Chrome, only the one with the proper markup directly in the HTML has the `disabled="disabled"` result when inspecing the rendered HTML. The rest all end with `disabled>`. Firefox shows similar
David Hedlund
results, but there, all but the first version gives you `disabled=""` (upon inspection in Firebug). in IE, on the other hand, all four versions are reported as having `disabled="disabled"` in the native development tools. An interesting side note is that in both Firebug and Chrome developer tools, the closing `/>` is stripped away even in the first input field, which is entirely untouched by the javascript.
David Hedlund
+1  A: 

The code you've shown definitely works:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Test</title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;
</head>
<body>
    <a href="#" onclick="$('#btn').attr('disabled', 'disabled')">Disable button</a><br/>
    <a href="#" onclick="$('#btn').removeAttr('disabled')">Enable button</a><br/>
    <input type="button" id="btn" value="Some Button" />
</body>
</html>
Darin Dimitrov