6

how to disable/enable a specific button by jquery ( by name )?

The button does not have ID , just this code:

<button onclick="$('ChangeAction').value='SaveAndDoOrder';" value="Bye" name="SaveAndDoOrder" type="submit">
        <i class="Icon ContinueIconTiny"></i>
        <span class="SelectedItem">Bye</span>
      </button>
1
  • 2
    You should try the search before making a new post, the last few days the same question has been asked a good few times. Commented Sep 24, 2010 at 10:37

7 Answers 7

11

I would recommend using ID instead of name, like:

$("#myButton").attr("disabled", true);

This will add the disabled attribute to the button which your browser will then automaticallly disable. If you want to use name you can use it like this:

$("button[name=myButton]").attr("disabled", true);
Sign up to request clarification or add additional context in comments.

1 Comment

The question asked for a way to disable/enable a button by name not by id.
7

Try this:

$("button[name=SaveAndDoOrder]").attr("disabled", "disabled");

That will disable a button with a name attribute equal to nameOfButton.

$("button[name=SaveAndDoOrder]").removeAttr("disabled");

That will remove the disable attribute from the same button.

1 Comment

thanks but this code does not work, see my updated html code above!
1

At least one answer has already been given which should work.

What I'd say though is that you'd be well advised to always have an ID on your elements, and not just rely on the name attribute. The name attribute is really only intended as a marker for posting form items.

CSS and the Javascript DOM are both optimised to look at IDs. You can do it by name, as in the answers you've already been given, but it's much less efficient - even if you can do it in the same amount of code, the browser will have to do more work behind the scenes.

Also some of the techniques for accessing elements by name are not available in all browsers (older versions of IE in particular have issues with [attribute] classes in CSS).

Comments

0

This is the code I use to disable or re-enable a control:

function handleControlDisplay(className, foundCount, checked) {



    var button = jQuery(className);



    if (foundCount > 0 && foundCount == checked) {

        // enable

        button.removeAttr("disabled");

    }

    else {

        // set the disabled attribute

        button.attr("disabled", "true");

    };

}

Comments

0

Create and use a jQuery plugin:

; (function($) {
    $.fn.enable = function(b) {
        return this.each(function() {
            this.disabled = !b;
        });
    };
})(jQuery);

use it like this

$(selector).enable(true|false);

Comments

0

Assuming the button is:

 <asp:Button runat="server" ID="btn1" Text="button!"/> 

then disabling will be done so:

$(document).ready  (function () {
    var a = $('#btn1');
    a.attr("disabled", function () {
        return "disabled";
    });     
}); 

Works perfectly

Comments

0

I use jQuery's prop():

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

To enable the button again, do:

$(this).prop('disabled', false);

somewhere in your code.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.