1
 $("#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");

2 Answers 2

1

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"></script>
</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>
Sign up to request clarification or add additional context in comments.

Comments

0

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);

3 Comments

This adds disabled="" instead of disabled="disabled" attribute which is the standard way.
@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://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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.