Target a Specific Child of an Element Using jQuery

Target a Specific Child of an Element Using jQuery
0

#1

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!


#2

Hi!

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


#3

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


#4

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.


#5

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.


#6

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.”


#8

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?


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

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


#10

addClass() fucntion should be written in camel case