Farok Ojil Farok Ojil - 4 months ago 13
jQuery Question

jquery - get text for element without children text

for example we have this file

<div id="mydiv">
some text here
<div id="inner div">
text for inner div
</div>
</div>


i need to get
#mydiv
text only with some code like this :

alert($('#mydiv').text()); // will alert "some text here"

Answer

hey try this please": http://jsfiddle.net/MtVxx/2/

Good link for your specific case in here: http://viralpatel.net/blogs/jquery-get-text-element-without-child-element/ (This will only get the text of the element)

Hope this helps, :)

code

jQuery.fn.justtext = function() {

    return $(this).clone()
            .children()
            .remove()
            .end()
            .text();

};

alert($('#mydiv').justtext());​
Comments