jquery sort out li

I have code

<ul id="list">
 <li>test</li>
 <li class="header">test</li>
 <li>test</li>
</ul>

I need execute this code

(use jquery)

var items = $("#list li");

$.each(items, function (key, value) {
 if ($(value).hasClass(".header")) { 
    ... Do Something ...
    }
}

this code doesn't work, because value == 'test'

Excuse, my English is awful

Answers


You are using a . in your hasClass functon while passing class name. Change it to:

var items = $("#list li");  
    $.each(items, function (key, value){  
        if ($(value).hasClass("header")){      
            alert('Hi');
            //Do Something
        } 
    });

I don't know what you mean by value == 'test'. There's nothing like that in your question.

Your $.each() is missing its closing ).

Also, your .hasClass should not include the . in the class name.

So this:

.hasClass(".header")

should be:

.hasClass("header")

Although, if you're interested in running code against only the items that have a class, just do a .filter() before .each().

items.filter('.header').each( function (key, value) {
    ... Do Something ...
});

If you have no other use for the collection, then you could modify the selector like this:

$('#list li.header').each( function (key, value) {
    ... Do Something ...
});

But only if you have no other need to cache all of the <li> elements.


Need Your Help

Custom Xcode IDE Plugin error: “Could not find class named…”

ios objective-c xcode cocoa-touch cocoa

I've developed my own custom Xcode .ideplugin to add a custom object to Xcode's Object Library pane. I have my custom object template based on a class I called IBMyCustomObject, which in turn has a

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.