Disabling asp Buttons by class name

Hello all!
I have two asp buttons that are in the header and footer of an asp DataList, and I am trying to use javascript to disable them based off a bool variable that is set in the codebehind. Because these buttons are in the datalist I thought it would be easier to use class names to find them. I have had no luck in getting the buttons disabled though. The following is what I have come up with and attempted.

    function SetButtonStatus() {

        var bb = document.getElementsByClassName('ButtonSubmit');
        for (var i = 0; i < bb.length; i++) {

            bb.disabled = "<%=!isQtyValid%>";
        }
    }

In the browser I am able to see that the bool variable isQtyValid is there. Is my problem with trying to getElementsByClassName? I did a check to see the length of what bb is and it returns 2 like I thought it would. Or is my problem that bb is a generic object and needs to be cast as a button?

Thanks for your time! Any tips or pointers are appreciated! If you think you need more info just let me know and I will do my best to get it to you.

Hi there,

Welcome to the forums :slight_smile:

getElementsByClassName() returns a node list and you have to iterate over that. This should work:

function SetButtonStatus() {
  var bb = document.getElementsByClassName('ButtonSubmit');
  for (var i = 0; i < bb.length; i++) {
    bb[i].disabled = "<%=!isQtyValid%>";
  }
}

Thanks! That made it do something more than what it was before!
For some reason it disables regardless. I went to the page source to see what the javascript said, and here is what it was:

    function SetButtonStatus() {
        var bb = document.getElementsByClassName('ButtonSubmit');
        for (var i = 0; i < bb.length; i++) {
            bb[i].disabled = "False";
  }
}

isn’t disabled = “false” supposed to mean its still enabled? even when it says bb[i].disabled = “True”; the buttons are disabled.

Again I do thank you! that one little error I had was keeping me from moving for a while.

Oops, sorry, the disabled attribute should be a Boolean.
You have it set to a string.
Try this:

    function SetButtonStatus() {
  var bb = document.getElementsByClassName('ButtonSubmit');
  for (var i = 0; i < bb.length; i++) {
    bb[i].disabled = false;
  }
}

I actually got it! I took the quotes off of the <%!isQtyValid%> then I looked in the console for the browser and it was telling me the False was undefined so then I did:

<%=(!isQtyValid).ToString().ToLower()%>;

and it works! I wouldn’t have gotten to this point if not for your help, I thank you for your help! :slight_smile:

You’re welcome :slight_smile:
Thanks for taking the time to report back.