Kyrbi
Kyrbi

Reputation: 2250

Disabling this button in jquery

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.

Upvotes: 1

Views: 114

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

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);
});

Upvotes: 7

Related Questions