Reputation: 2615
I'm creating a to-do-list web app, which sets each to-do as a cookie. I'm trying to loop through jQuery.cookie values to separate out my cookies into 'to-do' and 'done'.
I'm working with this code which at the moment gets all of the cookies, if the key is numeric then it will append the items to my tbody
var obj = $.cookie();
// add each one to the list!
$.each( obj, function( key, value ){
if ( $.isNumeric(key) ) {
// display the table
$('.grid-100').animate({
opacity: 1
}, 300);
// hide the intro page
$('.introduction').hide();
if( value.indexOf('class="done green"') ) {
// append to tfoot
$('tfoot').append(value);
} else {
// append to tbody
$('tbody').append(value);
}
} else {
// do nothing.
}
});
However this doesn't work, all of my to-do's get appended to tfoot
even if they don't have an indexOf
class="done green
.
Upvotes: 0
Views: 102
Reputation: 388316
indexOf
returns -1
if the item is not found, which is a truthy value, so you need to check whether the index is greater than -1 to test whether the item is found in the string.
if( value.indexOf('class="done green"') >= 0 ) {
Upvotes: 3