Click to See Complete Forum and Search --> : whats wrong with this code
Wyvern
06-19-2004, 05:16 PM
document.write("<a href='' onMouseOver='disp=1'>hello</a>");
if (disp==1){document.write("hello");}
i know it's probably Very wrong but it's my first peice of javascript where i've accually had to think. i;ve used other languages...but for some reason icant quike grasp how this on works lol.
it should start with the word "hello" then when you put the mouse over it another "hello" should appear next to it.
russell
06-19-2004, 05:26 PM
<script>
document.write('<a href="" onMouseOver="this.innerText=\'hello hello\'" onmouseout="this.innerText=\'hello\'">hello</a>');
</script>
apparently, the escape characters are filtered out by the forum...? You will need to escape the single quotes by placing a backslash before the single quotes in the innerText part.
You'll need to use a function onMouseOver that updates the inner text of an element on the page. The document.write function only works while the page is loading, not after.
Wyvern
06-19-2004, 05:37 PM
thanks for pointing that out :) thatexplains alot lol.what function should i use instead?
David Harrison
06-19-2004, 10:07 PM
Last time I checked, innerText wasn't part of the DOM, this is though:<script type="text/javascript"><!--
document.write('<a href="#" onmouseover="this.replaceChild(document.createTextNode(\\'Secondary Text.\\'),this.firstChild);" onmouseout="this.replaceChild(document.createTextNode(\\'Primary Text.\\'),this.firstChild);">Primary Text.</a>');
//--></script>In the code "this" references the current element.
The code also uses replaceChild, there are two arguements for it, which child node you want to replace and what you want to replace it with (not in that order).
document.createTextNode() does exactly what you'd think, it creates a text node so you can insert text into an element after it has loaded.
firstChild references the first (and only in this particular case) child node of the element, which happens to be a text node.
So what this script does is replace the link text with some other link text. It's a bit more long winded that than the first method offered, but this is the valid way to do it.
russell, to get a backslash to display you need to put two backslashes, so to escape a single quote in the code, you have to actually type this in \\\'
Originally posted by lavalamp
Last time I checked, innerText wasn't part of the DOM
It isn't, and I wasn't suggesting using it; I was using two words: "inner" and "text." I didn't say "innerText." ;)
David Harrison
06-20-2004, 09:39 AM
I didn't mention any names, I just stated a fact. ;)