Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Cosimo Scarpa
Cosimo Scarpa
14,047 Points

Selection between two <div> with JQuery

Hello guys, if I have two <div> and I want give an animation just at one of them, how can do it?

This is the code

$(document).ready(function(){
    $("button").click(function(){
        $("div")[0].animate({
        left: '250px',
        opacity: '0.5',
        height: '150px',
        width: '150px'
        });
    });
});

I tired with the brackets but does not work.

Thank you!

1 Answer

Stephan Olsen
Stephan Olsen
6,650 Points

If you only want to animate one div, you should only select one div. Right now you're using jQuery to select all divs on line 3.

$("div")

You should select the div you want to animate more specifically, for example by a class or an id.

$(document).ready(function(){
    $("button").click(function(){
        $("#myDiv")[0].animate({
        left: '250px',
        opacity: '0.5',
        height: '150px',
        width: '150px'
        });
    });
});