Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable/Non-Clickable an HTML button in Javascript

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.

like image 667
Azeem Avatar asked Mar 17 '14 14:03

Azeem


People also ask

How do I make a button not clickable in HTML?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How do I disable a button in JavaScript?

To disable a button using only JavaScript you need to set its disabled property to false . For example: element. disabled = true . And to enable a button we would do the opposite by setting the disabled JavaScript property to false .

How do I make a button not clickable in jquery?

ready(function() { $('input[type="button"]'). click(function() { var input = this; input. disabled = true; setTimeout(function() { input. disabled = false; }, 3000); }); });


3 Answers

Use :

resetBtn.disabled = "disabled";

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

like image 163
davidkonrad Avatar answered Sep 18 '22 08:09

davidkonrad


Using Only CSS:

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

<input class="no-click" />
like image 22
Abu Sufian Avatar answered Sep 19 '22 08:09

Abu Sufian


You can do it with the method : setAttribute()

Your js will be like that :

document.getElementById("reset").setAttribute('disabled','disabled');
like image 29
Tom R. Avatar answered Sep 21 '22 08:09

Tom R.