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()); 

js fiddle demo.

or:

var text = $('#item_1').map(function(){     return $.trim(this.firstchild.nodevalue); }).get().tostring(); 

js fiddle demo.

without jquery:

var text = document.queryselector('#item_1').firstchild.nodevalue.trim(); 

js fiddle demo.

references:


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -