Search code examples
phpjqueryhtmlmouseenter

jquery in html tag: on mouseover="myfunction();"


Is it possible to have a jquery mouseover in a html tag?

for(x=1; isset($_COOKIE["link$x"]); $x++)
    echo <div id="'.$x.'" onLoad="myfunction('.$x.')">
}

Like this example above but with "mouseenter" insted of "onLoad" ?

for(x=1; isset($_COOKIE["link$x"]); $x++)
    echo <div id="'.$x.'" OnMouseEnter="myfunction('.$x.')">
}    

and then in javascript

function myfunction(which){
    document.getElementById(which).style.backgroundColor = red;
}

Solution

  • As you mention in your comment, that you have multiple divs that should be fired on mouseenter, in this case you have to use "class" instead of "id"

    <div class="myDiv">container1</div>
    <div class="myDiv">container2</div>
    <div class="myDiv">container3</div>
    

    and in your jQuery code:

    $( ".myDiv" ).mouseenter(function() {
    $( this ).text( "mouse enter" );
    })
    
    $( ".myDiv" ).mouseleave(function() {
    $( this ).text( "mouse leave" );
    });