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">
</div>
';
However, everytime I move the mouse over the li element, nothing happens.
Does anyone know what I'm doing wrong?
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/
#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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With