How to get only direct text without tags with jQuery in HTML

The best way is to .clone() your object, .remove() all of its .children(), then go back to the object using .end() and finally get the .text().

The method is described in this blog post.

$("strong")
        .clone()    //clone the element
        .children() //select all the children
        .remove()   //remove all the children
        .end()  //again go back to selected element
        .text();    //get the text of element

Leave a Comment