Solved

Element name undefined?

Posted on 2003-10-27
6
1,886 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
  • 4
  • 2
6 Comments
 
LVL 3

Accepted Solution

by:
superslamwich earned 165 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Lol, or I could be wrong again :-)  It's fine in my first post.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 4

Author Comment

by:swinslow
Comment Utility
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
Comment Utility
ah, thats what did.  I was wondering why it worked...

{Slam}
0
 
LVL 4

Author Comment

by:swinslow
Comment Utility
LOL!
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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 can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now