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

JavaScript

creating a live image search with javascript and Jquery.

How would you write this javascript code so that the list containers display goes to none when index is equal to -1 ??

<div id="image-gallery" class="imageGalleryWrapper">
    <ul id="image-gallery" class="clearfix">
      <li class="firstImage"><a href="images/01.jpg"><img src="images/01.jpg" class="firstImage" alt="Loved the hay bales in this open field against the blue sky"></a></li>
      <li><a href="images/02.jpg"><img src="images/02.jpg" alt="Came across the still lake at the foot of beautiful mountains"></a></li>
      <li ><a href="images/03.jpg" ><img src="images/03.jpg" alt="Green praries have always been a favorite and with the small trees mixed in"></a></li>
      <li class="imgMargin" ><a href="images/04.jpg"><img src="images/04.jpg" alt="Cold but beautiful with snow capped mountains and floating ice on this lake scene"></a></li>
      <li><a href="images/05.jpg"><img src="images/05.jpg" alt="Amazing this sky behind these vibrant red cliffs,  couln't get enough of the moutains"></a></li>
      <li><a href="images/06.jpg"><img src="images/06.jpg" alt="Kind of a foggy day just outside of the forest and a bridge to tie it in"></a></li>
      <li><a href="images/07.jpg"><img src="images/07.jpg" alt="Farm field surrounded by deep green"></a></li>
      <li class="imgMargin"><a href="images/08.jpg"><img src="images/08.jpg"  alt="Windy sand dunes on this beach!"></a></li>
      <li ><a href="images/09.jpg"><img src="images/09.jpg" alt="Quiet country road nestled in by the trees"></a></li>
      <li><a href="images/10.jpg"><img src="images/10.jpg" alt="Ocean beating against the cliffs as the sun came up"></a></li>
      <li><a href="images/11.jpg"><img src="images/11.jpg" alt="Mountain cave overlooking a muddy river"></a></li>
      <li class="imgMargin lastImage" ><a href="images/12.jpg"><img src="images/12.jpg"  alt="Wild lavender and snow capped mountains bring back the memories"></a></li>
    </ul>
    </div>

```javascript

(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()
  }              

}());

Hey guys, I am working on a interactive photo gallery in my javascript course. Basically I have to create a search box where the images filter in real time if the user's input matches the specific images title/caption. So far I have created the light box but I have no Idea on how to program the this filter feature with jquery. If anyone have dealt with this problem and can give me some assistance it would be greatly appreciated. Thanks once again.

here is my html

<!DOCTYPE html>
<html>
<head>
<title>Jquery project</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="lightbox/src/imagelightbox.css" rel="stylesheet">
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>

<div id="content">

<input type="text" name="search" placeholder="Search(16pt)" data-search>

<div class="filtr-container">

 <ul class="thumbnails">

<li>
<a href="photos/01.jpg" title="Hay bayles" data-imagelightbox="demo" data-ilb2-caption="Hay Bales I love hay bales. Took this snap on a drive through the countryside past some straw fields">
<img src="images/01.jpg" alt="Hay Bales" title="hay bayles"></a>
</li>


<li>
<a href="photos/02.jpg" title="Lake" data-filter="all" data-imagelightbox="demo"  data-ilb2-caption="The lake was so calm today. We had a great view of the snow on the mountains from here">
<img src="images/02.jpg" alt="Lake" title="1st Photo"></a>
</li>



<li>
<a href="photos/03.jpg" title="Canyon"data-imagelightbox="demo" data-ilb2-caption="I hiked to the top of the mountain and got this picture of the canyon and trees below."><img src="images/03.jpg" alt="Canyon" title="1st Photo"></a>
</li>



<li>
<a href="photos/04.jpg" title="Iceberg"data-imagelightbox="demo" data-ilb2-caption="It was amazing to see an iceberg up close, it was so cold but didn’t snow today."><img src="images/04.jpg" alt="Iceberg
" title="1st Photo"></a>
</li>




<li>
<a href="photos/05.jpg" title="Desert" data-imagelightbox="demo" data-ilb2-caption="The red cliffs were beautiful. It was really hot in the desert but we did a lot of walking through the canyons."><img src="images/05.jpg" alt="
Desert" title="1st Photo"></a>
</li>




<li>
<a href="photos/06.jpg" title="Fall" data-imagelightbox="demo" data-ilb2-caption="Fall is coming, I love when the leaves on the trees start to change color.">
<img src="images/06.jpg" alt="Fall" title="1st Photo"></a>
</li>



<li>
<a href="photos/07.jpg" title="Plantation" data-imagelightbox="demo" data-ilb2-caption="I drove past this plantation yesterday, everything is so green!">
<img src="images/07.jpg" alt="Plantation" title="1st Photo"></a>
</li>




<li>
<a href="photos/08.jpg" title="Dunes" data-imagelightbox="demo" data-ilb2-caption="My summer vacation to the Oregon Coast. I love the sandy dunes!">
<img src="images/08.jpg" alt="Dunes" title="1st Photo"></a>
</li>





<li>
<a href="photos/09.jpg" title="Countryside" data-imagelightbox="demo" data-ilb2-caption="LaneWe enjoyed a quiet stroll down this countryside lane.">
<img src="images/09.jpg" alt="Countryside" title="1st Photo"></a>
</li>




<li>
<a href="photos/10.jpg" data-imagelightbox="demo" data-ilb2-caption="Sunset at the coast! The sky turned a lovely shade of orange.">
<img src="images/10.jpg" alt="Sunset" title="1st Photo"></a>\
</li>




<li>
<a href="photos/11.jpg" title="Cave" data-imagelightbox="demo" data-ilb2-caption="I did a tour of a cave today and the view of the landscape below was breathtaking.">
<img src="images/11.jpg" alt="Cave" title="1st Photo"></a>
</li>




<li>
<a href="photos/12.jpg" title="Bluebells" data-imagelightbox="demo" data-ilb2-caption="I walked through this meadow of bluebells and got a good view of the snow on the mountain before the fog came in.">
<img src="images/12.jpg" alt="Bluebells" title="1st Photo"></a>
</li>

</ul>




</div>

</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="lightbox/src/imagelightbox.js"></script>
<script src="js/apps.js"></script>



</body>
</html>

This is my javascript so far which is just code for the lightbox

$('a[data-imagelightbox="demo"]').imageLightbox({
arrows:true,
button:true,
overlay:true,
caption:true
});


var filterizd = $('.content-photos').filterizr({



});

2 Answers

Steven Parker
Steven Parker
243,318 Points

:blush: Oops, I missed the part about "list container".

So, using my JQuery-ized replacement, and observing that the image is inside an anchor (a) which itself is insde the list item, we can just change the toggle to affect the parent of the parent of the item, instead of the item (img) itself:

      $(img.element).parent().parent().toggle(index != -1);

just plugged that in works perfectly! I got that code from Jon Duckett's book. I still have to study it for a bit to find out why exactly this is so, but this clue will definitely open up the door for that. Thanks for your time and help.

Steven Parker
Steven Parker
243,318 Points

Isn't that what this line does already?

      img.element.style.display = index === -1 ? 'none' : '';  // Show / hide

But since you're using JQuery, I'm wondering why you didn't just do this:

      $(img.element).toggle(index != -1);

Hello Steven, when I key in text to the search field the images that do not match do not display but the <li> element container remains, this leaves the matching images in their original position as the <li> containers they are in. I am looking for code where the <li> containers disappear as well so that the matching images display in the top left of the main container instead of in their original column and row.