Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    change var onmouseover

    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?
    Code:
    <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.
    Last edited by stefanos1990; 12-29-2007 at 01:25 AM.

  • #2
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Found one error...

    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:

    Code:
     
    <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

  • #3
    New Coder
    Join Date
    Dec 2007
    Location
    NY, USA
    Posts
    27
    Thanks
    7
    Thanked 2 Times in 2 Posts

    Got It

    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.

    Code:
    <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!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •