Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Element name undefined?

Posted on 2003-10-27
6
Medium Priority
?
1,910 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 660 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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 
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

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.

Question has a verified solution.

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

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…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
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…

722 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