SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.


    Code JavaScript:
        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.

  2. #2
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Hi there,

    Welcome to the forums

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

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

  3. #3
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
        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.

  4. #4
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    Oops, sorry, the disabled attribute should be a Boolean.
    You have it set to a string.
    Try this:

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

  5. #5
    SitePoint Member
    Join Date
    May 2013
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code JavaScript:
    <%=(!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!

  6. #6
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,313
    Mentioned
    178 Post(s)
    Tagged
    8 Thread(s)
    You're welcome
    Thanks for taking the time to report back.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •