Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML jQuery
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 15-Nov-12 23:02pm
Lubomur313
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

 
$('.remove').click(function () {
var postHTML = $(this).closest(".post").html(); 
// select closest div or element with class post
   });
 
try this. its working fine.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can try this
 
$('.remove').live('click', function() {
 
var postHTML =$(this).parent('.post').html();
 
});
 
Thanks,
Jyotish
  Permalink  
v2
Comments
Lubomur at 16-Nov-12 7:58am
   
don't work
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

var post = $(this).parents().parents().html();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

 $('.remove').live('click', function () {
            $('.post:eq(1)').html('');
        });
 
Please try this this will give you the last div with class = post
  Permalink  
v2
Comments
Lubomur at 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 at 16-Nov-12 5:57am
   
Check updated solution..it will remove the second .post element

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 509
1 Nirav Prabtani 298
2 OriginalGriff 215
3 _Amy 165
4 Abhinav S 160
0 OriginalGriff 7,715
1 Sergey Alexandrovich Kryukov 6,911
2 Maciej Los 3,954
3 Peter Leow 3,698
4 CHill60 2,742


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100