jquery - How to get nearest text inside an element? -
$("#item_1").text() return text under #item_1, need "the first item".
is possible text "the first item" using jquery?
<ul> <li id="item_1"> first item <ul class="ltundertema _khkonteringundertemaer mt2"> <li>item 1.1</li> <li>item 1.2</li> </ul> <li>...</li> <li>...</li> </ul>
i'd suggest:
var text = $.trim($('#item_1').contents().filter(function(){ return this.nodetype === 3; }).text());
or:
var text = $('#item_1').map(function(){ return $.trim(this.firstchild.nodevalue); }).get().tostring();
without jquery:
var text = document.queryselector('#item_1').firstchild.nodevalue.trim();
references:
$.trim()
.contents()
.filter()
.get()
.map()
.
Comments
Post a Comment