Click to See Complete Forum and Search --> : innerHTML isn't working
scarleton
May 16th, 2003, 04:32 PM
For some reaons when I set innerHTML, it is not being displayed. I have this at the end of my HTML:
<div name="theName">This is a test...</div>
Then I hve javascript that looks like this and is called on a click of the mouse:
elReportDiv = document.getElementsByName(theName);
alert(elReportDiv.innerHTML);
elReportDiv.innerHTML = "<bold>Things are working</bold>";
The first time this code is called, "This is a test..." is in the alert box, the second time it contains "Things are working", but "This is a test..." is still being displayed on the screen.
Is there something I need to do to refresh the screen?
Sam
Nemi
May 19th, 2003, 05:33 PM
You've got the wrong forum, but your problem likely is caused from the fact that the getElementsByName returns an array of elements and not just one, if I remember right. Try this and see what it does
elReportDiv = document.getElementsByName(theName);
alert(elReportDiv[0].innerHTML);
elReportDiv[0].innerHTML = "<bold>Things are working</bold>";
This is untested and off the top of my head.
[Goodz13:Moved from Java]
scarleton
May 19th, 2003, 09:57 PM
I am going to have to agree with you. I switched to using GetElementById and it worked fine. It did not dawn on me that it would be a collection, though the name implies that:) Thank you for your insight!
Sam
P.S. What is the right forum?
Nemi
May 20th, 2003, 09:43 AM
The one its in now.
ideru
February 13th, 2006, 11:08 PM
I am resurrecting this thread, since I have the same problem :blush:
well am trying javascript and have this function on innerHTML
this is my javascript function
function changeLink()
{
alert("its here")
document.getElementById('myAnchor1').innerHTML ="Visit Microsoft"
document.getElementById('myAnchor1').href="http://www.microsoft.com"
document.getElementById('myAnchor1').target="_blank"
}
I put the alert part to check if the function is called when the button is clicked. But somehow it is not called
on the body of my html
<a id="myAnchor1" href="http://www.codeguru.com"> Visit CodeGuru </a>
<br /><br />
<input type="button" onclick="changeLink()" value="Change Link">
it does not seem to work?
did i miss anything :confused:
PramodsNair
February 14th, 2006, 12:08 AM
Dear ideru,
In my Win XP box with IE 6 this is working fine. The link changes as you intented when the button is clicked.
Pramod S Nair
ideru
February 14th, 2006, 01:10 AM
PramodsNair, I was able to make it work. ( just now ) :)
What I did was put all my function into a .js file instead of putting it inside my HTML file.
I'm using Firefox. Windows XP
PeejAvery
February 14th, 2006, 06:08 AM
What I did was put all my function into a .js file instead of putting it inside my HTML file.
Well, let's still figure out why. I noticed two things that you haven't done that should be in scripting.
Can you humor me and put the code back in the HTML and try my revisions?
1. Script tags. Did you just not copy and paste them in the example?
2. End each JavaScript line with a semicolon.
<script language="JavaScript">
function changeLink()
{
alert("its here");
document.getElementById('myAnchor1').innerHTML ="Visit Microsoft";
document.getElementById('myAnchor1').href="http://www.microsoft.com";
document.getElementById('myAnchor1').target="_blank";
}
</script>
drhowarddrfine
February 14th, 2006, 04:45 PM
I thought innerHTML only worked with IE.
PeejAvery
February 14th, 2006, 05:41 PM
I thought innerHTML only worked with IE.
Every browser but the Netscape 4 and under.
ideru
February 15th, 2006, 12:41 AM
Well, let's still figure out why. I noticed two things that you haven't done that should be in scripting.
Can you humor me and put the code back in the HTML and try my revisions?
1. Script tags. Did you just not copy and paste them in the example?
2. End each JavaScript line with a semicolon.
first thanks for the info peejavery.
I tried another one today. And I found the culprit why.
in my script tag
script type ="type/javascript" --> Error
script type ="text/javascript" --> OK
about the semicolon, this was explained in the tutorial that am using
Ending Statements With a Semicolon?
With traditional programming languages, like C++ and Java, each code statement has to end with a semicolon.
Many programmers continue this habit when writing JavaScript, but in general, semicolons are optional! However, semicolons are required if you want to put more than one statement on a single line.
PeejAvery
February 15th, 2006, 12:52 AM
Well, I figured it was the script tags. I only put...
<script language="JavaScript">
</script>
Concerning semicolons, yes, it is true that they are optional but that is only a guarantee in some browsers. Believe it or not, there are some browsers which will read errors without the semicolons. I always strongly suggest to everyone the adding of semicolons. It will help with code recognition, learning of other languages, and multibrowser support.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.