Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery mouseover not working for li element

I'm trying to make a menu based on an ul element that will show the link description underneath the link.

So far, I have the following code (simplified a little)

      echo '
<script>
        $("#menu_1").mouseover(function() {
            $("#description").replaceWith("test");
        });
</script>

<ul class="menu_top">
<li id="menu_1"><a href = "#">Test</a></li>
</ul>

<div id="description" class="menu_desc">&nbsp;
</div>

';

However, everytime I move the mouse over the li element, nothing happens.

Does anyone know what I'm doing wrong?

like image 795
BBog Avatar asked Feb 05 '26 06:02

BBog


2 Answers

You need to make sure that your event assignment happens after the document has finished loading, otherwise your mouseover event has nothing to attach too:

$(document).ready(function(){

$("#menu_1").mouseover(function() {
            $("#description").replaceWith("test");
        });

});

Here's a working demo: http://jsfiddle.net/2mWb3/

like image 153
Jamie Dixon Avatar answered Feb 07 '26 18:02

Jamie Dixon


#menu1 is not yet in the DOM when you run this code..

change it to

<script>
     $(function(){
        $("#menu_1").mouseover(function() {
            $("#description").replaceWith("test");
        });
      });
</script>

so that your code runs on document.ready event.

like image 30
Gabriele Petrioli Avatar answered Feb 07 '26 17:02

Gabriele Petrioli



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!