Solved

Element name undefined?

Posted on 2003-10-27
6
1,902 Views
Last Modified: 2013-11-19
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
Comment
Question by:swinslow
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
superslamwich earned 165 total points
ID: 9630854
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
 
LVL 3

Expert Comment

by:superslamwich
ID: 9630863
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
 
LVL 3

Expert Comment

by:superslamwich
ID: 9630866
Lol, or I could be wrong again :-)  It's fine in my first post.
0
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!

 
LVL 4

Author Comment

by:swinslow
ID: 9630905
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
 
LVL 3

Expert Comment

by:superslamwich
ID: 9630925
ah, thats what did.  I was wondering why it worked...

{Slam}
0
 
LVL 4

Author Comment

by:swinslow
ID: 9630939
LOL!
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question