getElementById multiple Id's

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • banning
    New Member
    • Aug 2007
    • 42

    #1

    getElementById multiple Id's

    ok ok i've read everywhere how using multiple id's is wrong and what not so don't get all hatefully, if there is a better way let me know... here is what i got

    [CODE=javascript]
    var click=0;

    function bgCOLOR(){
    if(click==0){
    click=1;
    }else{
    click=0;
    }

    if (click==1){
    document.getEle mentById('heha' ).style.backgro undColor='red';
    }else{
    document.getEle mentById('heha' ).style.backgro undColor='white ';
    }
    }

    <table>
    <tr>
    <td id="heha'" onclick="bgCOLO R();"></td>
    </tr>
    <tr>
    <td id="heha'" onclick="bgCOLO R();"></td>
    </tr>
    [/CODE]

    ok so my noob question is why wont it change the background color on both table divisions?

    im pretty entry level at javascript i primarly use just php but i figured its time to grow and start integrating the 2.

    anyway thanks for the help guys in advance.
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, Banning.

    Try using the name property, which does not have to be unique:
    [code=html]
    <td name="myName" ... >
    .
    .
    .
    <td name="myName" ... >
    [/code]

    [code=javascript]
    var tds = document.getEle mentsByName('my Name');
    for( var i = 0; i < tds.length; ++i )
    {
    tds[i].style.backgrou ndColor = 'annoyingly bright neon peach';
    }
    [/code]

    Comment

    • banning
      New Member
      • Aug 2007
      • 42

      #3
      wow you're just like every where on these boards haha... i really love getting help from ya you explain things very well

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Banning.

        We aim to please.

        Good luck with your project, and if you ever need anything, post back anytime :)

        Comment

        • markrawlingson
          Recognized Expert Contributor
          • Aug 2007
          • 346

          #5
          If i might suggest a slightly different method...

          [CODE=javascript]
          function bgCOLOR(oObj,sC olorOn,sColorOf f) {
          if (oObj.style.bac kgroundColor == sColorOn) {
          oObj.style.back groundColor = sColorOff;
          }
          else {
          oObj.style.back groundColor = sColorOn;
          }
          }
          [/CODE]

          Code:
          <tr>
          <td onClick="bgCOLOR(this,'#000000','#FF0000');">
          
          </td>
          </tr>
          This way you don't need to fiddle with any ids or names for these objects because you are refering to 'this' - ie: the object that fired the event (onClick) - you can also re-use this function over and over and apply any color to it on the fly.

          Comment

          • Romulo NF
            New Member
            • Nov 2006
            • 54

            #6
            [code=javascript]
            window.onload = function() {
            tds = document.getEle mentsByTagName( "td")
            for (x=0; x<tds.length; x++) {
            tds[x].onclick = colorIt
            }
            }

            function colorIt() {
            if (!this.color) { this.color = "off" }

            if (this.color == "off") {
            this.color = "on"
            this.style.back ground = "#000"
            }

            else {
            this.color = "off"
            this.style.back ground = "#fff"
            }
            }
            [/code]

            Or you can do something like that so you dont wanna to place function calls directly into the html!
            Last edited by pbmods; Aug 31 '07, 09:35 PM. Reason: Changed [CODE] to [CODE=javascript].

            Comment

            • banning
              New Member
              • Aug 2007
              • 42

              #7
              all of that up there... that i wrote... lol ignore it... i was lookin at your code... and like i said before i'm learning javascript, fortunatly its pretty close to AS but anyway... i say there is an onclick function thats pretty nifty... i may have to empliment that so i dont have functions right in the html :)

              Comment

              • banning
                New Member
                • Aug 2007
                • 42

                #8
                Originally posted by pbmods
                Heya, Banning.

                Try using the name property, which does not have to be unique:
                [code=html]
                <td name="myName" ... >
                .
                .
                .
                <td name="myName" ... >
                [/code]

                [code=javascript]
                var tds = document.getEle mentsByName('my Name');
                for( var i = 0; i < tds.length; ++i )
                {
                tds[i].style.backgrou ndColor = 'annoyingly bright neon peach';
                }
                [/code]
                i tried that code and it works great in firefox but it wont work in IE... i tried using
                Code:
                document.write(tds)
                after
                Code:
                 var tds = document.getElementsByName('myName');
                but before the
                Code:
                for( var i = 0; i < tds.length; ++i )
                and what i got in IE was [object] and in FireFox i get [object HTMLCollection]

                im trying to teach myself javascript and im sure there is something there that the document.write is telling me LOL i just dont know what to do with that information

                if you want to see the site im using it on you can find it here...

                Comment

                • pbmods
                  Recognized Expert Expert
                  • Apr 2007
                  • 5821

                  #9
                  Heya, Banning.

                  First off, you might like this:
                  &#91;CODE=javas cript]
                  JavaScript goes here.
                  [/CODE]

                  ~_^

                  Now then.

                  Does IE throw an error at you? Or does your script just seem not to do anything?

                  Comment

                  • banning
                    New Member
                    • Aug 2007
                    • 42

                    #10
                    Originally posted by pbmods
                    Heya, Banning.

                    First off, you might like this:
                    [CODE=javascript]
                    JavaScript goes here.
                    [/CODE]

                    ~_^

                    Now then.

                    Does IE throw an error at you? Or does your script just seem not to do anything?
                    Thanks about the code thing can I do CODE=php and stuff too?

                    and I don't get any errors coming up in IE it just doesn't do anything

                    Comment

                    • iam_clint
                      Recognized Expert Top Contributor
                      • Jul 2006
                      • 1207

                      #11
                      do this
                      [CODE=javascript]document.write( tds.length);[/CODE]

                      sometimes IE has a problem pulling names where the id's are the same.

                      the number it writes to the page should be > 0

                      Comment

                      • drhowarddrfine
                        Recognized Expert Expert
                        • Sep 2006
                        • 7434

                        #12
                        Originally posted by pbmods
                        Try using the name property, which does not have to be unique:
                        This is what 'class' is for. Just change all the ids to 'class='.

                        Comment

                        • banning
                          New Member
                          • Aug 2007
                          • 42

                          #13
                          Originally posted by iam_clint
                          do this
                          [CODE=javascript]document.write( tds.length);[/CODE]

                          sometimes IE has a problem pulling names where the id's are the same.

                          the number it writes to the page should be > 0

                          I tried it and it wrote back 0

                          it didnt write > 0

                          Comment

                          • banning
                            New Member
                            • Aug 2007
                            • 42

                            #14
                            Originally posted by drhowarddrfine
                            This is what 'class' is for. Just change all the ids to 'class='.

                            change all the name= to class= ?

                            if i do that then i cant use getElementsByNa me right? and as far as i know there is no getElementByCla ss

                            Comment

                            • drhowarddrfine
                              Recognized Expert Expert
                              • Sep 2006
                              • 7434

                              #15
                              No. Change the id's to class. id can only be used once per page. Class can be used for multiple elements.

                              Comment

                              Working...