beetle
beetle

Reputation: 223

List of li elements in jquery

I don't know why my code doesn't work.

$("#example").find('LI A').hasClass("sth").each(function(){alert($(this))});

Firebug says:

$("#example").find('LI A').hasClass("sth").each is not a function

The problem in this code is each, because if I delete it, it giving me no errors.

I need to pass founded value of "a" element to array.

Upvotes: 0

Views: 73

Answers (2)

Starx
Starx

Reputation: 78991

Your application of hasClass is incorrect. It does not return a jQuery Object, but a boolean, so .each() cannot be applied to it.

You have to attach the class to the selector

$("#find").click(function (){
    $("#example").find('a.sth').each(function(){
        $("#test").append($(this));
    });
 });

Demo

Upvotes: 2

gdoron
gdoron

Reputation: 150253

hasClass function returns boolean not a jQuery object. thus it doesn't have the each function.

You probably meant this:

$("#example").find('LI A.sth').each(function(){alert($(this))});

Or this (which is better):

$("#example li a.sth").each(function(){alert($(this))});

Read the docs:

.hasClass( className ) Returns: Boolean

Description: Determine whether any of the matched elements are assigned the given class

Upvotes: 4

Related Questions