0

Once my button gets clicked, it should get disabled and never be clicked again until the page is refreshed. Below is my code:

<html>
<head>
<script type="text/javascript">
function myButtonClicked()
{
    alert("Has myButton got disabled? I need solution for this.");
}
</script>
</head>
<body>
<button type="button" id="myButton1" onclick="myButtonClicked()">Click ME</button>
<button type="button" id="myButton2" onclick="myButtonClicked()">Click ME</button>
<button type="button" id="myButton3" onclick="myButtonClicked()">Click ME</button>
</body>
</html>

Before calling myButtonClicked function, it should get disabled. Actually, I want to write a PHP script in this function which will fetch some data from database which will take some time in real environment. That is why I want to disable the button. How do I achieve that?

1

7 Answers 7

7

You can pass this to your function:

<button type="button" id="myButton1" onclick="myButtonClicked(this)">Click ME</button>
<button type="button" id="myButton2" onclick="myButtonClicked(this)">Click ME</button>
<button type="button" id="myButton3" onclick="myButtonClicked(this)">Click ME</button>

then you can use:

function myButtonClicked(el)
{
    el.disabled = true; 
}

Fiddle Demo

with jQuery you can use .click() along with .prop():

$('button').click(function() {
    $(this).prop('disabled',true);
});

Fiddle Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Yup, gotta mention jQuery 'cause it solves every problem in the whole universe :P
@Felix - If I write alert("hello"); after el.disabled = true; in your fiddle, alert is not fired, although button is disabled.
@Felix - yes, it worked, sorry it was my mistake :). Thanks for the simple solution for my problem. Accepting your answer.
2
function myButtonClicked()
{
    $(this).prop('disabled', true);
}

Is it helping?

2 Comments

do you have jQuery included?
if its old version .prop may not work. try with .attr("disabled", true);
1

Add -

function myButtonClicked()
{
    alert("Has myButton got disabled? I need solution for this.");
    this.disabled = true;
}

Comments

1

Give your element as an argument within calling teh function

   <button type="button" id="myButton1" onclick="myButtonClicked(this)">Click ME</button>
   <button type="button" id="myButton2" onclick="myButtonClicked(this)">Click ME</button>
   <button type="button" id="myButton3" onclick="myButtonClicked(this)">Click ME</button>

And set the element attribute "disabled" to true

function myButtonClicked(obj) {
        obj.setAttribute("disabled", true);
    }

fiddle: http://jsfiddle.net/quJX8/

Comments

1

JSBIN

$("button").click(function() {
  $('#'+this.id).prop('disabled', true);
});

Comments

0

You can disable like this, this is self explainable:

<script>
function myButtonClicked(id)
{
    alert("Has myButton got disabled? I need solution for this.");

    $("#" + id).attr("disabled", true);
}
</script>

<body>
<button type="button" id="myButton1" onclick="myButtonClicked(this.id)">Click ME</button>
<button type="button" id="myButton2" onclick="myButtonClicked(this.id)">Click ME</button>
<button type="button" id="myButton3" onclick="myButtonClicked(this.id)">Click ME</button>
</body>

Comments

0

There are simpler ways than rewriting your function and passing "this". Just use "this" in your onclick attribute:

<button type="button" id="myButton1" onclick="this.disabled=true;myButtonClicked()">Click ME</button>

That will disable it, but it isn't necessarily that visibly obvious on the form. I like to use:

<button type="button" id="myButton1" onclick="this.style.visibility='hidden';myButtonClicked()">Click ME</button>

Which "disappears" it, but it still holds its place in the form.

If you don't need the placeholder, you can also use:

<button type="button" id="myButton1" onclick="this.hidden=true;myButtonClicked()">Click ME</button>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.