-1

So I currently have some code that I only want to run for a specific div called #photo3. It currently works as code when I have the first line say "body", but of course, this applies the code to the entire webpage. When I tried changing the "body" to "photo3" or "#photo3", the code stopped working. How can I apply this code only for a div called #photo3?

$("body").keydown(function(e){
    // left arrow
    if ((e.keyCode || e.which) == 37)
    {   
        document.body.style.overflow='auto';document.getElementById('photo3').style.display='none';document.getElementById('fade').style.display='none';
        document.body.style.overflow='hidden';document.getElementById('photo2').style.display='block';document.getElementById('fade').style.display='block';
    }
    // right arrow
    if ((e.keyCode || e.which) == 39)
    {
        document.body.style.overflow='auto';document.getElementById('photo3').style.display='none';document.getElementById('fade').style.display='none';
        document.body.style.overflow='hidden';document.getElementById('photo').style.display='block';document.getElementById('fade').style.display='block';
    }   
});

The HTML:

<div id="fade" class="black_overlay"></div>
<div id="photo" class="photo_content"> 
   <table class="photo_table">
      <img class="photoimg" src="images/photo.jpg">
   </table>
</div>
<div id="photo2" class="photo_content"> 
   <table class="photo_table">
      <img class="photoimg" src="images/photo2.jpg">
   </table>
</div>
<div id="photo3" class="photo_content"> 
   <table class="photo_table">
      <img class="photoimg" src="images/photo-food.jpg">
   </table>
</div>
Jay
  • 357
  • 3
  • 8
  • 18

1 Answers1

2

To select a specific div you can use either $("div#photo3") or just $("#photo3").

You can also do the same with classes and few other attributes by replacing the # as required (. for class, for example).

Hope that helps!

JohnHoulderUK
  • 639
  • 2
  • 8
  • 27