this question has answer here:
- get text node of element 8 answers
i have html external website need parse jquery. i'm interested in text inside main div (like described in code).
the problem is, if use $('#main').text()
texts inside div, nested divs.
<div id="main"> <div><h1>....</h1></div> <div>other text</div> want parse text <div></div> <div></div> </div>
any advice?
with jquery can use contents()
give access node elements inside div, after can filter textnodes:
var nodetext = $('#main').contents().filter(function(){ return this.nodetype === 3 && $.trim(this.nodevalue) !== ''; }).text() console.log(nodetext)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="main"> <div><h1>....</h1></div> <div>other text</div> want parse text <div></div> <div></div> </div>
Comments
Post a Comment