...

View Full Version : change var onmouseover



stefanos1990
12-29-2007, 01:20 AM
hi,

I am new to javascript, so apologies for asking a probably easy question, but i couldn't find the solution elswhere.

why doesn't this code work?

<script type="text/javascript">
var fruit = apple;
</script>

<a href="#" onmouseover="fruit=banana;">change var fruit to banana</a>
<a href="#" onmouseover="fruit=grape;">change var fruit to grape</a>
<a href="#" onmouseover="fruit=orange;">change var fruit to orange</a>

<script type="text/javascript">
document.write (fruit);
</script>


ps. I guess the output of the document.write part doesn't change because the page needs to be refreshed or something like that, but that's not really the problem, i want to change the value of the var, the document.write part is to check if it has changed.

333qaz333
12-29-2007, 05:37 AM
Hey,
I am new also, but I found one of the problems here.
You are assigning a string to the variable fruit, so apple, banana etc need to be in hash marks. EX:



<script type="text/javascript">
var fruit = 'apple';
</script>

<a href="#" onmouseover="fruit = 'banana';">change var fruit to banana</a>
<a href="#" onmouseover="fruit = 'grape';">change var fruit to grape</a>
<a href="#" onmouseover="fruit = 'orange';">change var fruit to orange</a>

<script type="text/javascript">
document.write (fruit);
</script>


I am still working on how to do the onmouseover part. give me some time.

Hope this helps

333qaz333
12-29-2007, 05:54 AM
Okay, well I got a code, I am not sure if it is the most effective, but it works.

I have a working example here (http://333qaz333.no-ip.biz/text2.php).



<html>
<head>
<script type="text/javascript">
var fruit = 'apple';
function writeFruit() {
document.getElementById('dispHere').innerHTML = fruit;
}
</script>
</head>
<body onload="writeFruit()">
<a href="#" onmouseover="fruit = 'banana'; writeFruit()">change var fruit to banana</a><br>
<a href="#" onmouseover="fruit = 'grape'; writeFruit()">change var fruit to grape</a><br>
<a href="#" onmouseover="fruit = 'orange'; writeFruit()">change var fruit to orange</a><br>
<p id="dispHere"></p>
</body>
</html>


Hope this helps!!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum