Solved

Element name undefined?

Posted on 2003-10-27
6
1,908 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

617 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