A Leonard
A Leonard

Reputation: 33

Trying to close a div from another div

I have created a listings page where each listing has its own div, the content for that div is loaded from a separate php script via a $.post

The problem I have is trying to hide the listing, I think the problem may be something to do with this line

$('div.close_listing')

only I can get it to work if I use

$('div.listing> div').click(  
function() {
$('div.listing> div').hide(); 
});

(which works when I click anywhere in the listing div)

Any help much appreciated.

The html code for the listing is:

<div id='listing'>
   <div id='loading'></div>
   <a name='13'><h3>Business1</h3></a> <h4 data-id=18-3-3>Name Of Business 1</h4>
    <div>
        <hr>
     *** content forbusiness1 appears here ***
          <div class='close_listing'>CLOSE this listing</div>
    </div>

   <a name='14'><h3>Business2</h3></a> <h4 data-id=19-3-3>Name Of Business 2</h4>
    <div>
        <hr>
     *** content for business2 appears here ***
          <div class='close_listing'>CLOSE this listing</div>
    </div>

</div>

The code to call the content is as follows:

$(document).ready(function() { 
$('div.listing> div').hide();  
$('div.listing> h4').click(function() {
$('div.listing> div').empty().html('<img src="loading_image.gif" /><br>Retrieving Details.....');
$.post("http://www.example.com/record.php", { id: $(this).attr('data-id') });

$.post('show.php',{ id: $(this).attr('data-id')}, function(data) {
$('div.listing> div').html(data); 

$('div.listing .close').visible();
});

var $nextDiv = $(this).next();
var $visibleSiblings = $nextDiv.siblings('div:visible');

if ($visibleSiblings.length ) {
  $visibleSiblings.slideUp('fast', function() {
    $nextDiv.slideToggle('fast');
  });
} else {
   $nextDiv.slideToggle('fast');
} 
});

// closes the listing down

$('div.close_listing').click(  
function() {
$('div.listing> div').hide(); 
}); 

});

Upvotes: 0

Views: 84

Answers (3)

Mark Adesina Omoniyi
Mark Adesina Omoniyi

Reputation: 449

I find it a bit difficult to understand your question, but if what you want is to close the close_listing parent div then I have edited your html markup like so

 <div id='listing'>
   <div id='loading'></div>
   <a name='13'><h3>Business1</h3></a> <h4 data-id=18-3-3>Name Of Business 1</h4>
    <div>
        <hr>
     content forbusiness1 appears here
          <a class='close_listing'  href="#">
            CLOSE this listing
          </a>
    </div>

   <a name='14'><h3>Business2</h3></a> <h4 data-id=19-3-3>Name Of Business 2</h4>
    <div>
        <hr>
      content for business2 appears here

           <a class='close_listing'  href="#">
            CLOSE this listing
          </a>
    </div>

</div>

The the javascript should be like so

$('a.close_listing').bind('click', function () {

    $(this).parent().show().fadeOut(500);
            return false;
    });

Upvotes: 0

iddo
iddo

Reputation: 749

The problem is this line:

$('div.listing> div').hide(); 

It should be

$('div#listing > div').hide();

But I think then you are hiding to much (all the divs directly below #listing). Consider the following:

$(this).parent().hide();

Upvotes: 0

aziz punjani
aziz punjani

Reputation: 25776

I'm guessing you intended to do this

$('div.close_listing').click(function() {
        $(this).closest('.listing').hide(); 
    }); 
});

Upvotes: 1

Related Questions