jQuery Selector Problem (All Children inside Div)
13,048
Solution 1
$("#better a").bind('click', function() { $(this).addClass('active'); });
Solution 2
If I understand what you're asking.. you want to end up with this?
<div id="better">
<a href="1.aspx" class="active">One</a>
<a href="2.aspx" class="active">Two</a>
<a href="3.aspx" class="active">Three</a>
</div>
If any link in that div was clicked, right? (you did say ALL anchor tags)... then use this:
$("#better").click(function() {
$(this).find('a').addClass('active');
return false;
});
Author by
Alex
Updated on June 13, 2022Comments
-
Alex almost 2 years
I'm trying to attach a click function that adds a CSS class to all anchor tags inside of a specific div. Somehow I can't get jQuery to do this (no JS error either).
The div:
<div id="better"> <a href="1.aspx">One</a> <a href="2.aspx">Two</a> <a href="3.aspx">Three</a> </div>
My jQuery code (inside of $(document).ready):
$("#better > a").click(function() { $(this).addClass('active'); });
I added an alert in my click function to see if it fires, but nothing happens. As I wrote above, I don't get JavaScript errors in the FF error console either.
Help?
Edit: This is just example code. Yes it doesn't make sense to change a class when you redirect. I've just simplified my code to the core of what I'm trying to do. :)