Target a Specific Child of an Element Using jQuery

Im stuck at this exercise

it says this:

You’ve seen why id attributes are so convenient for targeting with jQuery selectors. But you won’t always have such neat ids to work with.Fortunately, jQuery has some other tricks for targeting the right elements.jQuery uses CSS Selectors to target elements. target:nth-child(n) css selector allows you to select all the nth elements with the target class or element type.Here’s how you would give the third element in each well the bounce class:$(".target:nth-child(3)").addClass(“animated bounce”);Make the second child in each of your well elements bounce. You must target the children of element with the target class.

and i wrote this
$("#target:nth-child(2)").addClass(“animated bounce”);

I dont know whats wrong
please help!

Hi!

Hint: think about with what you need to replace ‘#’ in: $("#target:nth-child(2)").addClass(“animated bounce”);

with a dot. But I dont know why Because target is an id, not a class

So, it looks like you already know how to select classes, id’s, etc. in jquery.

Can you post a screen shot or all of the challenge code? It’s hard to see where the discrepancy is without seeing the whole challenge.

In that case, you should do that:

$("#target3").addClass("animated bounce");
$("#target6").addClass("animated bounce");

Then you accessing by ID. And completing that task (if there would no req for “nth-child”

But you need to use “nth-child(2)”, and targeting CLASS.

$(".target:nth-child(2)").addClass(“animated bounce”);

One line instead of two.

Ok, another way. You just “oversaturated” by “target” everywhere. Check this:

    <button class="btn btn-default trgt" id="target1">#target1</button>
    <button class="btn btn-default trgt" id="target2">#target2</button>
    <button class="btn btn-default trgt" id="target3">#target3</button>

One of two group, I simple changed class to “trgt” everywhere.

So, code like $(".trgt:nth-child(2)").addClass(“animated bounce”);
should be a bit more clear for an eye. “Class “trgt”, child second.”

1 Like

I’m stuck here too, it must be something I’m not seeing. I know this is an old question but is anybody still here?

$(".target:nth-child(2)").addClass("animated bounce");

Why does this code work? what is “.target” referring to? Shouldn’t it be “.well”?

3 Likes

addClass() fucntion should be written in camel case