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
  • 1,992
  • 6
  • 19
  • 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
  • 132,029
  • 27
  • 254
  • 262
Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303