Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1918
  • Last Modified:

Element name undefined?

This has got to be an easy one I am sure, and I am missig something simple.
With the code I have below, I am getting a JavaScript error "TestData" is undefined.
Thanks in Advance,
Soren
Here is the code.........
<html>
 
  <head>
 
  <title>Test Document</title>
       
   <script language="JavaScript" defer="true">

       TestData.innerHTML = "$12,345,678.90";

   </script>

  </head>

  <body>

        <div name="TestData"></div>
 
  </body>
 
  </html>
 
0
swinslow
Asked:
swinslow
  • 4
  • 2
1 Solution
 
superslamwichCommented:
You can't refer to objects like that in javascript.  Instead of just using the name, you need to use

<html>
 
 <head>
 
 <title>Test Document</title>
     
   <script language="JavaScript" defer="true">

      document.getElementById('TestData').innerHTML = "$12,345,678.90";

  </script>

 </head>

 <body>

       <div name="TestData" id="TestData"></div>
 
 </body>
 
 </html>

{Slam}
0
 
superslamwichCommented:
whoops, need to change one more thing as well.  You're calling the script to change the innerHTML before the element has been loaded.  Since the script is above it, it is executed before the div exists, and it can't do anything.  Move the script to the bottom of the page after the div for it to work
0
 
superslamwichCommented:
Lol, or I could be wrong again :-)  It's fine in my first post.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
swinslowAuthor Commented:
I knew it would be something stupid. I was using name="TestData" instead of id="TestData".
In the <script> tag, defer="true" keeps the JavaScript from running until the page is loaded.
Soren


0
 
superslamwichCommented:
ah, thats what did.  I was wondering why it worked...

{Slam}
0
 
swinslowAuthor Commented:
LOL!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now