Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJoshua Kelly
18,830 Pointssearch functionality not working
I know i am making this a lot more difficult than it should be, yet i am getting the error that indexOf() is not working properly.
$('.page-header').append('<div class="student-search"><input class="sInput" placeholder="Search for students..."/><button class="sSearch">Search</button><div>');
var students = document.getElementsByTagName('li');
var studArr = jQuery.makeArray(students);
var searchObj = {};
$('button.sSearch').on('click', function() {
console.log('clicked');
var searchFor = $('.sInput').val().toLowerCase();
if (searchFor === '') {
$('.pagination').remove();
paginate(studArr);
$('a:eq(0)').trigger('click');
} else {
$('.student-item').each(function() {
if ($(this).indexOf(searchFor) >= 0) {
$(this).detach().appendTo(searchOjb);
}
});
$('.student-list').remove();
paginate(searchObj);
$('a:eq(0)').trigger('click');
searchObj = {};
};
});
I am not having issues with any of the functionality below.
function pages(fArr, show) {
var pageNum = Math.floor((fArr.length / show) + 1);
return pageNum;
}
function paginate(nArr) {
var buttons = pages(nArr, 10);
var $pageDiv = '<div class="pagination"><ul>';
for ( var i = 0; i < buttons; i++ ) {
$pageDiv += '<li><a href="#">' + (i+1) + '</a></li>';
}
$pageDiv += '</li></div>';
$('.page').append($pageDiv);
$('a').on('click', function(nArr) {
$('a').removeClass('active');
$(this).addClass('active');
$('.student-item').each(function() {
$(this).addClass('hide');
});
var idx = $(this).parent().index() * 10;
for ( var i = 0; i < 10; i++) {
$('.student-item:eq(' + idx + ')').removeClass('hide');
idx++;
}
});
}
paginate(studArr);
$('a:eq(0)').trigger('click');
1 Answer
ALFRED MOHENU
1,499 PointsIn the if statement, you are appending to searchOjb instead of searchObj
if ($(this).indexOf(searchFor) >= 0) {
$(this).detach().appendTo(searchOjb);
Instead of
if ($(this).indexOf(searchFor) >= 0) {
$(this).detach().appendTo(searchObj);
Joshua Kelly
18,830 PointsJoshua Kelly
18,830 PointsThanks. Good catch. Still telling me indexOf() is not a method. i don't know why.