I have the following jQuery:
$(".score-window a:first-child").click(function() {
$(".score-window").hide();
$(".login-window").show();
});
$(".score-window a:nth-child(2)").click(function() {
$(".score-window").hide();
$(".register-window").show();
});
Which is hooked up to the following HTML:
<div class="score-window">
<i class="icon-remove" title="Close"></i>
<p>In order to view your score, you have to <a href="#">log in</a>.</p><br>
<p>Don't have an account yet? <a href="#">Register</a>! Totally free and you'll get the ability to save your scores.</p>
</div>
I only have two links in the score-window class, so I don’t see why this isn’t working.
You have two links, but each one is the only child of its parent
p, so they will matcha:first-childonly. The parent of the links is not.score-window, butp. However the parent of thepelements (along with theiandbrelements) is.score-window.You need to amend your selectors to use
:nth-child()with thepelements instead, then select theaunder each one. There’s aniwhich is the first child, and abrbetween your twopelements which looks like it isn’t needed. You should be able to remove it then do this:If the
brmust stay there for whatever reason, usep:nth-child(4)orp:last-childinstead for your second selector.If you’re using or can upgrade to jQuery 1.9, you can use
:nth-of-type()instead to limit the count to just yourpelements (i.e. the firstpand the secondp), but older versions of jQuery don’t support it: