-2

I've been working on a fadein fadeout image slideshow. I've used hoverizr to take advantage of the greyscale effect. Catch is, when the image is on the current slide, thumb should be colored image.

Based on the given scenario, I've been trying to add/remove classes after every slide. I have successfully added the greyscale class on all images but wasn't able to remove the greyscale on the current image.

Question is, is it possible to pass element ID using javascript then retrieve it in jQuery? Code Sample Below:

 <script>
 function updateThumbs(element_id){
      var elem = element_id;
      $('#slide a img').addClass('greyScale')
      $('#' + elem).removeClass('greyScale');
 }
 </script>
 <div id="slide">
      <a onClick="updateThumbs('image_1');">
        <img src="..." id="image_1" class="greyScale">
      </a>
      <a onClick="updateThumbs('image_2');">
        <img src="..." id="image_2" class="greyScale">
      </a>
 </div>
Barlas Apaydin
  • 7,233
  • 11
  • 55
  • 86

1 Answers1

1

Yes, you can use jQuery in this manor. However, I would wire up events in document.ready instead of the html. And you can put the click event on the image instead of the anchor.

$(document).ready(function() {
    $("#slide img").click(function () {
        $('#slide img').addClass('greyScale');
        $(this).removeClass('greyScale');
    });
});

Complete example at ​jsFiddle

Also notice the missing semi-colon in your addClass() statement.

jrummell
  • 42,637
  • 17
  • 112
  • 171