Click here to Skip to main content
11,478,896 members (75,938 online)
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 16-Nov-12 0:02am
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 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

var post = $(this).parents().parents().html();
  Permalink  

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 200
1 F-ES Sitecore 195
2 Frankie-C 120
3 OriginalGriff 105
4 DamithSL 95
0 Sergey Alexandrovich Kryukov 7,880
1 OriginalGriff 7,341
2 Sascha Lefèvre 3,064
3 Maciej Los 2,491
4 Richard Deeming 2,335


Advertise | Privacy | Mobile
Web03 | 2.8.150520.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2015
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