Click here to Skip to main content
15,896,912 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, how can I select "post" div html() by click "remove".
If I write $('post').html() select work only for first "post", but I need to select for example second or third.

//html
<div class="posts">
    <div class="post">
       Some text for first div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for second div
       <a class="remove">remove</a>
       <hr>
    </div>

    <div class="post">
       Some text for third div
       <a class="remove">remove</a>
       <hr>
    </div>
</div>

// jQuery
$('.remove').live('click', function() {
    var postHTML = $(this).html(); // select only "a" element
});
Posted

JavaScript
$('.remove').click(function () {
var postHTML = $(this).closest(".post").html(); 
// select closest div or element with class post
   });


try this. its working fine.
 
Share this answer
 
v2
You can try this

$('.remove').live('click', function() {

var postHTML =$(this).parent('.post').html();

});

Thanks,
Jyotish
 
Share this answer
 
v2
Comments
Lubomur 16-Nov-12 7:58am    
don't work
JavaScript
$('.remove').live('click', function () {
           $('.post:eq(1)').html('');
       });


Please try this this will give you the last div with class = post
 
Share this answer
 
v2
Comments
Lubomur 16-Nov-12 5:22am    
It's work, but if I want to get second div?

The sink .find() must help with selection, if we provide all posts unique id like post1 or post2, we can use:
$('.posts').find('.post2').html();
but it's don't work!
Ravi Tuvar 16-Nov-12 5:57am    
Check updated solution..it will remove the second .post element
var post = $(this).parents().parents().html();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900