Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Jquery Show just one Item by the same class after click-event

i am new in scripting Jquery/Javascript and actually i’ve some struggle.
How can i show one item by the same class without affect the other items?

My code:

$(function() {
  $('.embedContainer > .myPosterImage').on('click', function() {
    $('.embedContainer > .myPosterImage').hide();
    $('.embedContainer > embed').show();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="embedContainer">
  <img class="myPosterImage" src="img/websites/01_documentation.jpg" data-original="img/websites/full/01_full.jpg" />
  <embed src="img/websites/Concept.pdf" width="563.41" height="375.98"></embed>
</div>

<div class="embedContainer">
  <img class="myPosterImage" src="img/websites/01_documentation.jpg" data-original="img/websites/full/01_full.jpg" />
  <embed src="img/websites/Concept.pdf" width="563.41" height="375.98"></embed>

</div>

Thanks a lot.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The first line will hide all of them, which may be what you want to do (if you want to collapse all and expand the clicked one, for example):

$('.embedContainer > .myPosterImage').hide();

But as you’ve found, this second line will also show all of them:

$('.embedContainer > embed').show();

What you can do is traverse the DOM starting from the element which was clicked (this within the click event handler). In this case you’d traverse up the DOM (using closest() for example) to a common parent element and then back down (using find() for example) to the target element.

Something like this:

$(this).closest('.embedContainer').find('embed').show();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading