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?
Javascript code is executed as it is encountered. This might happen before the document is actually ready. In your case, you’re trying to select the
#menu_1element before it actually exists in the document. Because you almost certainly want all your jQuery code to run after the document is ready, jQuery provides a shortcut method for you: