Web Development with jQuery®

(Elliott) #1

Filtering Selections and Arrays (^) ❘ 149
The preceding markup document is linked to the following style sheet:
body {
font: 12px "Lucida Grande", Arial, sans-serif;
color: rgb(50, 50, 50);
margin: 0 ;
padding: 0 10px;
}
ul {
list-style: none;
margin: 0 0 10px 0 ;
padding: 10px;
background: yellow;
width: 250px;
}
ul li {
padding: 3px;
}
h4 {
margin: 10px 0 ;
}
li.rubberSoulJohnAndPaul {
background: lightblue;
}
ul#rubberSoulFiltered {
display: none;
}
The following script demonstrates the grep() method:
$(document).ready(
function()
{
var songs = [];
$('ul#rubberSoul li').each(
function()
{
songs.push(
$(this).text()
);
}
);
var filteredSongs = $.grep(
songs,
function(value, key)
{
return value.indexOf('You') != -1;
}
);
var ul = $('ul#rubberSoulFiltered');
http://www.it-ebooks.info

Free download pdf