> Where would the code that you've listed go?
jQuery is really designed to be separate from the HTML content; so
with that being said, it is typically found in the head section of the
document.
Now, you can't just put that in the head section and expect it to
work, because it will fire before the elements actually exist. In
traditional Javascript, this problem was solved by attaching events to
the window.onload event handler. The only issue with this method, is
the window.onload event fires after the entire page loads, meaning
your script would not execute until after every html element, image,
etc is loaded.
jQuery provides a solution for this, $(document).ready() method. This
method executes immediatly after the DOM and only the DOM finishes
loading. With that being said, you can do the following:
function myFunction()
{
// Insert Code
}
$(document).ready(myfunction)
however, or you can just pass an anonymous function like this:
$(document).ready(function(){
// Insert Code
});