This seems pretty simple, but I can’t figure it out. I’m new to javascript/ jquery.
i want to trigger a function to move a div onmouseover, and another onmouseout. I want to call this same function on several different divs. how would I do this without writing multiple functions?
<div id="indexJoinBanner" onmouseover="moveDivRight()" onmouseout="moveDivLeft()">
<!-- end #indexJoinBanner --></div>
<div id="indexJoinBanner2" onmouseover="moveDivRight()" onmouseout="moveDivLeft()">
<!-- end #indexJoinBanner2 --></div>
function moveDivRight(){
$("#indexJoinBanner").animate({
left: "0px",
},500 );
};
function moveDivLeft() {
$("#indexJoinBanner").animate({
left: "-150px",
},500 );
}
Thanks
You can pass the div to the function like
and use it in the function
thisin the attributes specify the current element.Alternatively you can attach handler using
.on()instead of onmouseover and onmouseout attributes, give all the elements you want to attach the handler a class thenDEMO