having some trouble with getElementById

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gsherp
    New Member
    • May 2007
    • 34

    having some trouble with getElementById

    I can't seem to get getElementById to work when
    I create an element in an external js file.


    example...
    [CODE=javascript]function blah(){
    var temp=createElem ent("input");
    temp.id = "stuff";
    document.append Child(temp); }[/CODE]


    my html page...

    [HTML] <script> window.onload = blah; </script>
    <script> document.getEle mentById('temp' ).value = "HELLO"; </script>
    [/HTML]

    result,
    I see the input field in the webpage, but not the value.
    Last edited by acoder; Sep 13 '07, 04:40 PM. Reason: Added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Moved from Articles section.

    Please remember to use code tags when posting code.

    As to your problem, you set the id to "stuff" and then you're trying to access the element with the id "temp".

    Comment

    • gsherp
      New Member
      • May 2007
      • 34

      #3
      thanks.

      I tried your suggestion and still does not work.

      Comment

      • gsherp
        New Member
        • May 2007
        • 34

        #4
        I set the id to "stuff" and then you're trying to access the element with the id "stuff"...d oes not work.

        Comment

        • Death Slaught
          Top Contributor
          • Aug 2007
          • 1137

          #5
          Originally posted by gsherp
          I can't seem to get getElementById to work when
          I create an element in an external js file.


          example...
          [CODE=javascript]function blah(){
          var temp=createElem ent("input");
          temp.id = "stuff";
          document.append Child(temp); }[/CODE]


          my html page...

          [HTML] <script> window.onload = blah; </script>
          <script> document.getEle mentById('temp' ).value = "HELLO"; </script>
          [/HTML]

          result,
          I see the input field in the webpage, but not the value.
          I think this is your problem:

          window.onload = blah;
          try changing it to this:

          Code:
          window.onload="blah();"
          if that doesn't work try getting rid of the "()" your window.onload is looking for the function "blah()".

          hope it helps, Death

          Comment

          • gsherp
            New Member
            • May 2007
            • 34

            #6
            when I add the "()" to the window.onload call of blah, the input box does not even appear anymore. if I don't the "()" the input box id appears, the the getElementById does not find it.

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Show your updated code.

              Comment

              • gsherp
                New Member
                • May 2007
                • 34

                #8
                Here is my updated code.

                Code: ( javascript )
                [CODE=javascript]function blah(){
                var temp=createElem ent("input");
                temp.id = "stuff";
                document.append Child(temp); }
                [/CODE]


                my html page...


                Code: ( html4strict )
                [HTML]<script type="text/javascript" src="blah.js">
                <script type="text/javascript"> window.onload = blah; </script>
                <scripttype="te xt/javascript"> document.getEle mentById('stuff ').value = "HELLO"; </script>
                [/HTML]
                any suggestions?
                Last edited by acoder; Sep 13 '07, 05:40 PM. Reason: added proper code tags

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  The proper way to use code tags is like this:&#91;code= javascript]Javascript code goes here[/code]

                  On line 3, the script and type should have a space:[code=html]<script type="...">..</script>[/code]

                  Comment

                  • gsherp
                    New Member
                    • May 2007
                    • 34

                    #10
                    I added the space between script and type, but still does not fix my problem.

                    Comment

                    • acoder
                      Recognized Expert MVP
                      • Nov 2006
                      • 16032

                      #11
                      Originally posted by gsherp
                      I added the space between script and type, but still does not fix my problem.
                      Aah, right. It's a classic case of trying to access something before it has been loaded into the DOM.

                      The element is added after the page is loaded, while you're trying to set the value of the (as yet non-existent) element during page load.

                      Comment

                      • gsherp
                        New Member
                        • May 2007
                        • 34

                        #12
                        So is there any other way i can implement this?

                        Comment

                        • acoder
                          Recognized Expert MVP
                          • Nov 2006
                          • 16032

                          #13
                          Originally posted by gsherp
                          So is there any other way i can implement this?
                          Either move it into blah() or swap the creating and accessing around, i.e. create th element during page load and set the value onload.

                          Comment

                          • gsherp
                            New Member
                            • May 2007
                            • 34

                            #14
                            thanks for all the help. I will try this out. Make sense.

                            Comment

                            • acoder
                              Recognized Expert MVP
                              • Nov 2006
                              • 16032

                              #15
                              OK, let me know if you get it working.

                              Comment

                              Working...