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 trial
Michael Williamson
Front End Web Development Techdegree Graduate 23,903 Pointsactive search filter in Javascript ??
I got this code from a book. and the problem is its code for img elements not having parents(my code has the anchor and list parent) so that when text is entered, the img containers set to display: none; and I'm left with all the "li" containers which keep the matched image in its original positions not top left of the "ul" list container like I'm trying. I've tried a lot of stuff, any suggestions?
<div class="imageGalleryWrapper">
<ul id="image-gallery" class="clearfix">
<li><a href="images/01.jpg"><img src="images/01.jpg"></a></li>
<li><a href="images/02.jpg"><img src="images/02.jpg"></a></li>
<li><a href="images/03.jpg"><img src="images/03.jpg"></a></li>
<li><a href="images/04.jpg"><img src="images/04.jpg"></a></li>
<li><a href="images/05.jpg"><img src="images/05.jpg"></a></li>
<li><a href="images/06.jpg"><img src="images/06.jpg"></a></li>
</ul>
</div>
//Image Search Filter
(function() { // Lives in an IIFE
var $imgs = $('#image-gallery img'); // Get the images
var $search = $('#searchBox'); // Get the input element
var cache = []; // Create an array called cache
$imgs.each(function() { // For each image
cache.push({ // Add an object to the cache array
element: this, // This image
text: this.alt.trim().toLowerCase() // Its alt text (lowercase trimmed)
});
});
function filter() { // Declare filter() function
var query = this.value.trim().toLowerCase(); // Get the query
cache.forEach(function(img) { // For each entry in cache pass image
var index = 0; // Set index to 0
if (query) { // If there is some query text
index = img.text.indexOf(query); // Find if query text is in there
}
img.element.style.display = index === -1 ? 'none' : ''; // Show / hide
});
}
if ('oninput' in $search[0]) { // If browser supports input event
$search.on('input', filter); // Use input event to call filter()
} else { // Otherwise
$search.on('keyup', filter); // Use keyup event to call filter()
}
}());
1 Answer
Steven Parker
243,318 PointsThis code contains references to things that aren't here. It might just be incomplete. Are you using a workspace? You could make a snapshot of your workspace and post the link to it here.
But in the meantime, a few observations:
- this code apparently uses JQuery .. is JQuery loaded?
- is this entire code block a self-executing anonymous function? Then that last line should probably be:
})();
If these don't fix it, perhaps the rest of the code and/or the snapshot will shed some light.