Reputation: 885
How I can select (in JavaScript) a specific label where the labels are generated from a database and don't have a unique identifier.
The selector of the label is: #content1 > div:nth-child(2) > label
. I have tried the following but getting an error
Error: Syntax error, unrecognized expression:
$("#content1 > div:nth-child(2) > label").click(function(){
$( "#bestdeals1 > div:nth-child(2)" ).addClass( "greenbestdeals" );
});
Upvotes: 1
Views: 50
Reputation: 885
The problem was in the function it self.
Changing the behavior to : $(document).on("click", "#content1 > div:nth-child(2) > label", function(evt) {}
Made it work. Thanks for the answers provided.
Upvotes: 1
Reputation: 5997
Maybe try this:
$('#content > div').eq(2).find('label').click(function() {
// ...
});
Upvotes: 1
Reputation: 2007
In case you weren't already using it that way, nth-child starts at 1 and not 0, which could be causing the error.
You can also use jQuery functions to find the children, like this:
$('#content1').find('div').eq(1).children('label').first()
You can replace find with children assuming what you're going for is an immediate descendant - find will go however many levels deep it needs to until it finds the target.
Upvotes: 1