Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable button in jQuery

Tags:

jquery

My page creates multiple buttons as id = 'rbutton_"+i+"'. Below is my code:

<button type='button' id = 'rbutton_"+i+"' onclick=disable(i);>Click me</button> 

In Javascript

function disable(i){     $("#rbutton'+i+'").attr("disabled","disabled"); } 

But it doesn't disable my button when I click on it.

like image 850
user2047817 Avatar asked Feb 27 '13 21:02

user2047817


People also ask

How do I make a button disabled?

The disabled attribute is a boolean attribute. When present, it specifies that the button should be disabled. A disabled button is unusable and un-clickable. The disabled attribute can be set to keep a user from clicking on the button until some other condition has been met (like selecting a checkbox, etc.).

How disable submit button until form is filled jQuery?

click(function () { if ($('#submit-button').is(':disabled')) { $('#submit-button'). removeAttr('disabled'); } else { $('#submit-button'). attr('disabled', 'disabled'); } }); In the above code, enabled is the id of the checkbox used and submit-button is the class name of the submit button used .


1 Answers

Use .prop instead (and clean up your selector string):

function disable(i){     $("#rbutton_"+i).prop("disabled",true); } 

generated HTML:

<button id="rbutton_1" onclick="disable(1)">Click me</button> <!-- wrap your onclick in quotes --> 

But the "best practices" approach is to use JavaScript event binding and this instead:

$('.rbutton').on('click',function() {      $(this).prop("disabled",true);  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>  <button class="rbutton">Click me</button>

http://jsfiddle.net/mblase75/2Nfu4/

like image 52
5 revs Avatar answered Dec 09 '22 16:12

5 revs