0

$(document).ready(function() { 
   
    $('.btn').click(function() {
        $(this).parent().addClass('show');	
    });
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
   <div class="row">
       <h4 id="txt1">Bookmark 1</h4>
       <a href="#" class="bokmarksave"> <img src="https://vignette.wikia.nocookie.net/secondlife/images/2/2b/Box.png/revision/latest?cb=20120226073326" class="hidden" width="100"></a>
      <p> Lorem Ipsum is simply dummy text of the printing and </p>
       <p class="page_num">Page 3 </p>
   </div>
   <div class="bookmark_icon"> 
      <a href="#"><img src="http://www.pngall.com/wp-content/uploads/2016/05/Click-Here-PNG-Images.png" width="100"class="btn"></a> 
   </div>
</div>

when we click the "click here" image with ".btn" class. I want to add a ".show" class to the image with ".hidden" class. I tied this with jquery but I can only add a class to the parent element.

2
  • After you add the show class, do you want to remove the previous hidden class? Commented Feb 24, 2018 at 20:05
  • why not just remove the hidden class instead of adding a show class ? Commented Feb 24, 2018 at 20:05

4 Answers 4

1

Try this:

$(this).closest('.container').find('.hidden').addClass('show');

However, a better practice would be to add some data- attributes to the appropriate elements in your HTML, and navigate using them. This could mean something like this:

$(this).closest('[data-container]').find('[data-bookmark-image').addClass('show');

edit: as @sheriffderek mentioned, removing the hidden class would probably make sense as well/instead.

Sign up to request clarification or add additional context in comments.

1 Comment

jsfiddle.net/sheriffderek/t0LLeuo5 - you can also just remove the class. : )
0

Just use the following JS:

$('.btn').click(function() {

  $(".hidden").addClass('show');

});

The image with class 'hidden' is selected as $(".hidden");

Comments

0

The parent element for the img is the a tag. Move the btn class to the a element and it will still works.

Comments

0

In this specific case, you can just target the element with the hidden class by using the related jQuery selector.

$(document).ready(function() { 
    $('.btn').click(function() {
       $('.hidden').toggleClass('show');
    });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.