Can this code be cleaned? Multiple objects triggering different hover events When the user visits the web page and hovers over an album artwork, the title of the piece and the artist name appears. It leaves when the mouse leaves.
I have achieved this with the following code:
HTML:

    &lt;img id="pic1" src=" "&gt;
&lt;div class="info1"&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song title&lt;/div&gt;
&lt;/div&gt;
&lt;img id="pic2" src=" "&gt;
&lt;div class="info2"&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song2&lt;/div&gt;
&lt;/div&gt;


JQuery

    $(document).ready(function () {

    $("img#pic1").hover(

    function () {
        $("div.info1").animate({
            'opacity': 1
        });
    },

    function () {
        $("div.info1").animate({
            'opacity': 0
        });
    });
});
$(document).ready(function () {

    $("img#pic2").hover(

        function () {
            $("div.info2").animate({
                'opacity': 1
            });
        },

        function () {
            $("div.info2").animate({
                'opacity': 0
            });
        });
    });


The problem is with the javascript; I have to manually add new parts when I add a new image and it takes up a lot of space. Is there a way to condense the code?

CSS:
    .info1, .info2 {
    opacity:0;
}

http://jsfiddle.net/burntember/22grA/
In your hover method you want to select the next two divs after the img. Assuming that the divs are located (in DOM) immediately after the img, then you can select them using the jQuery .next() method, instead of selecting them by name.

ALso instead of selecting each img by id, you could give them all the same class, and then use a single jQuery selector to select every img with that class.

Untested and probably slightly buggy example code:

&lt;img id="pic1" class="album" src=" "&gt;
&lt;div&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song title&lt;/div&gt;
&lt;/div&gt;
&lt;img id="pic2" class="album" src=" "&gt;
&lt;div&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song2&lt;/div&gt;
&lt;/div&gt;

$(document).ready(function () {

    $("img.album").each().hover(

        $div = $(this).next(); // find the next element after this img

        function () {
            $div.animate({
                'opacity': 1
            });
        },

        function () {
            $div.animate({
                'opacity': 0
            });
        });
    });


If the div element is not immediately after the img (so you can't use the .next method), you can relate each div to its img by defining a new attribute which begins with data-, for example data-img-id:

&lt;img id="pic1" class="album" src=" "&gt;
&lt;img id="pic2" class="album" src=" "&gt;

&lt;div data-img-id="pic1"&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song title&lt;/div&gt;
&lt;/div&gt;

&lt;div data-img-id="pic2"&gt;
    &lt;div class="name"&gt;artist&lt;/div&gt;
    &lt;div class="songname"&gt;song2&lt;/div&gt;
&lt;/div&gt;


Then, in your general-purpose hover method, you can get the id of the this img tag, and use that id value to select the corresponding div using its data-img-id element.