I need alert thisparenttext in following code by clicking on clickedlicontent
<script>
$("#one").live("click",function(){
var partxt=$(this).parent().parent().text();
alert(partxt);
});
</script>
<ul>
<li>thisparenttext<ul><li id="one">clickedlicontent</li></ul></li>
<li>bb</li>
</ul>
In other word, I want take only the text of first parent of clicked li, not all its all html codes.
What you need to do is clone the element, remove all it’s children and use
$.text()to get the remaining text:This takes the parent of the parent and clones it. This is so that when we do
.children().remove()it doesn’t affect the displayed elements..end()will “re-select” the parent of the parent of$(this)and.text()gets the text.