0

There's a lot of tutorials about hide/show functions in jQuery but can't find any regarding my issue. I'm not new to programming, jquery is a first for me.

I would like it when a user clicks on "link 1" etc...it hides the word "hide me" below. Currently it partially works, but it only hides the actual link...not the "hide me" word. I know it's to do with a selector...but not sure where to put the selector in.

Thanks.

Simple js fiddle

$(document).ready(function(){
//user clicks on link
    $('a').click(function(){
         //hides the word below link but doesn't work
        $(this).hide();
    });


});

I've tried

 $(this).hide("panel");

and

  $("panel", this).hide();

Thanks

Huangism
  • 16,278
  • 7
  • 48
  • 74
Superunknown
  • 501
  • 2
  • 10
  • 30

5 Answers5

3

Its not working because, when you say $(this) it will take object of clicked element only and that's anchor tag:

use this:

$(this).closest("div").hide();

Instead of

$(this).hide();

Help your self: try:

console.log($(this));

//This will show what does that $(this) take:

Update:

Based, on your html structure, You can use something like this:

$(this).parent().next().hide()

DEMO

$(document).ready(function(){
    $('a').click(function(){
   
         $(this).parent().next().hide();
    });
   
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
 <div id="wrap">
  <div id="main">
   
   
   <div class="row">
    <div class="column">
     <p><a href="#" title="Click me!">Link 1</a></p>
    
     <div class="box">
      <p>Hide me</p>
     </div>
    </div>
    
    <div class="column">
     <p><a href="#" title="Click me!">Link 2</a></p>
    
     <div class="box">
      <p>Hide me</p>
     </div>
    </div>
    
    <div class="column">
     <p><a href="#" title="Click me!">Link 3</a></p>
    
     <div class="box">
      <p>Hide me </p>
     </div>
    </div>
   </div>
  </div>
 </div>
Mox Shah
  • 2,967
  • 2
  • 26
  • 42
3

Change:

$(this).hide();

to:

$(this).parent().next('.box').hide();

$(this) is hiding what you're clicking

StudioTime
  • 22,603
  • 38
  • 120
  • 207
2

What about:

$(this).closest('.column').find(".box").hide();
Huangism
  • 16,278
  • 7
  • 48
  • 74
blablabla
  • 1,468
  • 15
  • 17
1

please look at what panel stands for. in this

$("panel").hide();

you are trying to hide all tags that aare panel try using id

$("#panel").hide();

or class name

$(".panel").hide();

or if this is a child node of "this" read in https://api.jquery.com/children/

ben f
  • 31
  • 1
  • 5
1

You can achieve this with closest function and hide childrens with .box class. Jsfiddle demo based on your code.

$(document).ready(function(){
    $('a').click(function(){
        $(this).closest('.column').children('.box').hide();
    });  
});
adricadar
  • 9,971
  • 5
  • 33
  • 46