20

I have following HTML snippet for a button:

HTML:

<div class="Clear" title="Clear">
   <div class="ClearButton">
      <button id="reset" type="reset" title="Clear Photos"></button>
   </div>
   <div class="ClearText">
      Clear
   </div>
</div>

CSS:

div.ClearButton
{
   vertical-align: top;
   display: inline-block;
   background: url(../CustomControl/buttons.png?ver=365321878) no-repeat scroll -343px -443px rgba(0, 0, 0, 0);
   height: 16px;
   overflow: hidden;
   width: 16px;
}

div.Clear
{
   vertical-align: top;
   display: inline-block;
   overflow: hidden;
   cursor: pointer;
   padding-left: 2px;
   padding-bottom: 6px;
   padding-right: 6px;
   padding-top: 4px;
}

On a certain event, I need to disable this button completely. I tried with the following code, but it does not disable/gray out the button and it's still clickable.

var resetBtn = document.getElementById("reset");
resetBtn.disabled = true;

As someone suggested that the CSS I have applied is causing this issue. Please suggest how can I make this button non-clickable.

4
  • Setting .disabled on the element should work. Is it not? Is this code executing at all? Is resetBtn being populated with a DOM element at all? Commented Mar 17, 2014 at 14:15
  • Yes code is executing and resetBtn is being populated with a DOM element. Commented Mar 17, 2014 at 14:16
  • That should work, .disabled = true is a way to disable a button. Furthermore, it works in this fiddle: jsfiddle.net/ZJeC4 which probably means that you are calling it too early (button does not exist yet). Commented Mar 17, 2014 at 14:16
  • try this --- $('#reset').attr("disabled", "disabled"); Commented Apr 29, 2016 at 7:36

7 Answers 7

14

Use :

resetBtn.disabled = "disabled";

This works in all browsers -> http://jsfiddle.net/fMV4B/

Sign up to request clarification or add additional context in comments.

Comments

11

Using Only CSS:

//CSS
.no-click {pointer-events: none;}

<input class="no-click" />

2 Comments

this is the simplest answer
This will still leave the button focusable and clickable by means of the keyboard.
4

You can do it with the method : setAttribute()

Your js will be like that :

document.getElementById("reset").setAttribute('disabled','disabled');

Comments

2

This JSFiddle shows it working, based on this: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_disabled. It could be that it's not working as expected because your CSS is making it visually appear different. Remove you CSS first to make sure the JavaScript it working as expected.

Here's the code for the JSFiddle:

<button type="button" id="test">Click Me!</button>

<script>
document.getElementById("test").disabled = true;
</script>

Do you have an example of when your JavaScript is running?

5 Comments

I am thinking the same way. I have placed all CSS in my post. Can you please suggest a way to disable button completely. The disable button code works when I remove CSS.
I'm guessing your background image (../CustomControl/buttons.png?ver=365321878) needs changing when you set 'disabled' to true. The easiest way to do this is to create a 'div.ClearButtonDisabled' class that has a different background image, slightly greyed out. Without being able to see the image and style of the button it's hard to know for sure.
good idea, i will do that but how can I remove hand-shaped cursor when I move the cursor over button?
A Button shouldn't have a hand icon. Even when clickable the button should have a default arrow pointer. In your CSS you're setting 'cursor: pointer' - this is where the hand-shaped cursor is coming from. In your disabled CSS class, set 'pointer: auto;'
use event.stopPropagation() in your event
1

Have you read through this answer or tried

https://stackoverflow.com/a/3014678/2992661

resetBtn.setAttribute('disabled', 'disabled');

3 Comments

Note that setAttribute is different than changing the property via .disabled =. I think it should still work the same, though.
True, I'd just taken that from the linked article to save the OP the time :)
setAttribute does mostly make sense if the attribute not is natively supported.
0

Your code works fine.

The problem is almost certainly that the JavaScript code is either not being called at all or is being called in the wrong spot.

If you run this code immediately, make sure that the DOM is loaded (window.onload callback, or preferably have your JS code right before </body>).

Otherwise just make sure that any event that runs the JS code is actually firing.

1 Comment

JS runs fine. I have used alerts to verify this. I think the CSS I have applied is not allowing disabled property to work.
0

Try this code:

To disable the submit button with jQuery, you just need to add a “disabled” attribute to the submit button.

$('#buttonId').click(function(){

    $('#buttonId').attr("disabled", true);  
});

To enable back the disabled submit button, set the “disabled” attribute to false, or remove the “disabled” attribute:

$('#buttonId').attr("disabled", false); 
or
$('#buttonId').removeAttr("disabled");

Comments

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.