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,212
  • 7
  • 25
  • 42

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
  • 143,310
  • 32
  • 282
  • 281
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339