1

I am wondering if someone can help me out with this.

I have a button defined as:

<input type="button" id="myButton" name="myButton" value="ClickMe!!" onClick="callMe()"/>

I can use jQuery, standard javascript or Dojo to disable the button with onClick event:

$('#myButton').attr('disabled', 'disabled');

The problem I am facing is, even though this code disables the button, onClick event still triggers the function callMe() if I click on the button.

How do I make the disabled button not call the onClick function?

John Slegers
  • 45,213
  • 22
  • 199
  • 169
t0mcat
  • 5,511
  • 19
  • 45
  • 58

8 Answers8

4

With jQuery you can bind a function that checks if your button is disabled:

$('#myButton').click(function() {
  if (!$(this).is(':disabled')) {
    callMe();
  }
});
ldiqual
  • 15,015
  • 6
  • 52
  • 90
1

Since you're using jQuery, use

$('#myButton').click(function() { callMe(); this.unbind(); });

instead of onClick.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180
0

Instead of ...

$('#myButton').attr('disabled', 'disabled');

... use ...

$('#myButton')
   .prop('disabled', 'disabled')  // Sets 'disabled' property
   .prop('onclick', null)         // Removes 'onclick' property if found
   .off('click');                 // Removes other events if found
John Slegers
  • 45,213
  • 22
  • 199
  • 169
0
$('#myButton').click(function(){
    if( !$(this).is('[disabled=disabled]') ){
       ...
    }
});
Michael
  • 1,058
  • 10
  • 17
0

In the same code where you disable it, simply remove the onclick event handler.

Jonathan M
  • 17,145
  • 9
  • 58
  • 91
0

you should be using jQuery to attach the click handlers, not adding them inline*

Just add a check to your click function

$('#myButton').click(function(){
  var $this;
  $this = $(this);
  if ( $this.is(':disabled') ) return;
  callMe();
});

Alternatively,

$('#myButton').click(callMe);

callMe()
{
  var $this;
  $this = $(this);
  if ($this.is(':disabled')) return;
  ...the rest of your code...
}

Or if you insist on using it inline:

onclick="if ($(this).is(':disabled')) return; callMe();"

* I regularly rant about how HTML, CSS & JS fit the definition of MVC

Community
  • 1
  • 1
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
  • Hi zzzzzBov, I dont insist on using inline. I would prefer the better solution, if you say inline isnt good, I wont use it :) – t0mcat Aug 22 '11 at 19:00
  • @t0mcat, I've updated my answer with links to the reasons why you should avoid inline JS. – zzzzBov Aug 22 '11 at 19:10
0

instead of using onclick attribute, bind to the event

$('#myButton').click(function(e){
    // try it without this
    if($(this).attr('disabled'))
        return false;
    callMe();
    e.preventDefault();
});

try it without the check, not sure if its necessary.

Juan Ayala
  • 3,388
  • 2
  • 19
  • 24
0

This is one way of doing.

$('#myButton').click(function(){
  if(!$(this).hasClass("disabled")){
     //Do stuff here
  }
});

To disable the buttons just add the disabled class to it.

$('#myButton').addClass("disabled");

Add appropriate styles in the disabled class to make button look like disabled or you can also set the disabled property along with setting the class.

ShankarSangoli
  • 69,612
  • 13
  • 93
  • 124