Forum Moderators: open

Message Too Old, No Replies

somewhat new to JS need a bit of help

I need a bit of help w/ my JS

         

bwassink

4:12 pm on Oct 23, 2009 (gmt 0)

10+ Year Member



I'm trying to create a menu where when you hover on something it changes the class. I've accomplished this already but what I would like to do is to leave it on mouse out and change it back when you mouse into another menu item. This is the code I have thus far.

<script language="JavaScript">
function reset(){
document.getElementById("1").style.class = "class1";
document.getElementById("2").style.class = "class1";
document.getElementById("3").style.class = "class1";
document.getElementById("4").style.class = "class1";
document.getElementById("5").style.class = "class1";
}
</script>

<div style="height:1.2em; ">
<div id="1" class="class1" onmouseover="reset;doAnimation(this.id);this.className='rounded'" >Home</div>
<div id="2" class="class1" onmouseover="reset;doAnimation(this.id);this.className='rounded'" >link2</div>
<div id="3" class="class1" onmouseover="reset;doAnimation(this.id);this.className='rounded'" >link3</div>
<div id="4" class="class1" onmouseover="reset;doAnimation(this.id);this.className='rounded'" >link4</div>
<div id="5" class="class1" onmouseover="reset;doAnimation(this.id);this.className='rounded'">link5</div>
</div>

doAnimation is a piece of script that runs a menu as dictated by hovered upon div. Thanks for any help.

Fotiman

5:22 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
Take a look at how you're setting the class name. When you are setting it to rounded, you're doing this:

this.className='rounded'

Where this is the element. But when you are setting it in your reset function, you're doing this:

document.getElementById("1").style.class = "class1";

Where document.getElementById("1") will be the element. Notice the difference?
.style.class
vs.
.className

bwassink

6:33 pm on Oct 23, 2009 (gmt 0)

10+ Year Member



Those are actually two separate actions. What my onmouseover is trying to accomplish is 1)reset- Reset all divs to class1 2)doAnimation - do the animation outlined in another code and 3)this.className='rounded' - set the class to rounded. I was hoping that by using this set of actions that 1 would make all divs=class1 2 would run the animation and 3 would then make the current div=rounded

Fotiman

6:42 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Right, but look at HOW you are setting the class value. In one example you're using:
element.style.class = "..."
and in another you're using:
element.className = "..."

bwassink

6:47 pm on Oct 23, 2009 (gmt 0)

10+ Year Member



Alright well as I said, I'm somewhat new to this so thanks for your help. I understand that the syntax is apparently wrong. Even after changing both to match, using element.className="..." its still having the same effect being, nothing. I guess the point I'm trying to make right now, is that if I'm doing something wrong I don't know enough to know what that something is.

bwassink

7:35 pm on Oct 23, 2009 (gmt 0)

10+ Year Member



my issue has been resolved... It was brought up to me that I needed to have "reset()" in my onmouseover statement.

Fotiman

7:42 pm on Oct 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Ok, looking more closely I see a few problems.

1. Your elements all have ID values that start with a number. You can't do that. ID values must begin with letter.

2. You are missing parenthesis after reset in your onmouseover handlers. Should be reset(); instead of reset;

3. You'll want to use the element.className="..." method for setting the class name.

Hope this helps.