1

I have got this button <button id="beg" onclick"disable()">beg</button>. And function is defined here

function disable(time){
    $(this).attr('disabled', 'disabled');
};

Problem is that button doesn't get disabled, I have no idea why.

Thank you.

Kyrbi
  • 2,102
  • 6
  • 22
  • 40

1 Answers1

7

It's because this in your function is not a reference to the button element. Try this:

<button id="beg" onclick="disable(this)">beg</button>
function disable(el){
    $(el).attr('disabled', 'disabled');
};

Or better yet, separate your HTML and JS completely by hooking up your event in JS:

<button id="beg">beg</button>
$('#beg').click(function() {
    $(this).prop('disabled', true);
});
VisioN
  • 138,460
  • 30
  • 271
  • 271
Rory McCrossan
  • 319,460
  • 37
  • 290
  • 318