onMouseOver to change/hide text

C

CQMMAN

Hello,

I am trying to change a piece of text using onmouseover. I have tried a few
things so far and nothing has really worked. I get get a new piece of text
to appear but bastardizing someone elses code, but can't figure out how to
hide the other one....

I sort of have the following but don't know if this is close to the best way
of doing it really...

Basically I want to have a piece of text, lets say TEXT1, that changes from
blue to red, and to TEXT2 while the mouse is over it.

I know nothing really about Java so have had a look around but got no
futher. Any help would be great.

Cheers




<html>
<head>
<script type="text/JavaScript">
function setText(t){
if(document.layers)
{
with(document.layers["myDiv"].document)
{
open();
write(t);
close();
}
}
else if(document.all)
{
myDiv.innerHTML = t;
}
}
</script>
</head>
<body>
<a href="#"
onmouseover="setText('test2)",
onmouseout="setText('')">test1</a>&nbsp;


&nbsp;<br>
<div style="position:absolute;top:14;left:11;width:100;" name="myDiv"
id="myDiv"></div>
</body>
</html>
 
C

CQMMAN

SanJoseWebGuy said:
You might try this too:
<p onMouseOver="this.style.color='#FF0000', this.innerHTML='The Text
Changed' "
onMouseOut ="this.style.color='#000000', this.innerHTML='Change My
Color' "
Change My Color
</p>

Thanks for the tip. Sorry, thought I can't get it to work.. What else do I
need with your snippet? I really don't know javascript at all..

Cheers
 
C

CQMMAN

SanJoseWebGuy said:
You might try this too:
<p onMouseOver="this.style.color='#FF0000', this.innerHTML='The Text
Changed' "
onMouseOut ="this.style.color='#000000', this.innerHTML='Change My
Color' "
Change My Color
</p>


Sorry to be such a pain, but that looks like it should do what I want, but
when I paste it into a n HTML document, it displays "Change My Color", but
doesn't actually do anything.

Do I need to define style or colour, or innerHTML somewhere?

Thanks
 
L

Lasse Reichstein Nielsen

I would change the "," to a ";". While legal, commas are used to
separate expressions (and evaluates to the last expression) while
semicolons are used to to end statements. In the above, the value of
the second (assignment-)expression is not relevant, so it's more
appropriate to treat it as a statement.
Sorry to be such a pain, but that looks like it should do what I want, but
when I paste it into a n HTML document, it displays "Change My Color", but
doesn't actually do anything.

Does it give a Javascript error message? Have you turned Javascript error
messages on? (If not, do so!)

I can see your quote has line breaks all over the place. You can not
have line breaks inside Javascript strings, so try putting more of it on
one line.

It works for me if I do.
Do I need to define style or colour, or innerHTML somewhere?

No, they already exist on the element.

/L
 
C

CQMMAN

Does it give a Javascript error message? Have you turned Javascript error
messages on? (If not, do so!)

I can see your quote has line breaks all over the place. You can not
have line breaks inside Javascript strings, so try putting more of it on
one line.

It works for me if I do.

Aaahhh.. Yes, linewrap.... Thanks very much for your help..

Cheers
CQMMAN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top