Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTMLjQuery
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 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 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  

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

  Print Answers RSS
0 Dnyaneshwar@Pune 1,464
1 Sergey Alexandrovich Kryukov 806
2 OriginalGriff 478
3 sanket saxena 313
4 Tadit Dash 305
0 Sergey Alexandrovich Kryukov 11,700
1 OriginalGriff 6,980
2 Peter Leow 4,915
3 Abhinav S 3,838
4 Maciej Los 3,575


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