Wednesday, 15 April 2015

javascript - How to get element text without its children -



javascript - How to get element text without its children -

how should element text without text within children?

example:

<div> hi <div> asd </div> </div>

i want 'hi'.

my code it's in jquery , it's working, need have same function in javascript (without jquery):

object.name = $html.find('.title').clone().children().remove().text();

thanks much.

once reference div, jquery i'm assuming has class title

class="snippet-code-js lang-js prettyprint-override">var div = document.getelementsbyclassname('title')[0]; //get reference parent div var name = div.firstchild.nodevalue; alert(name) class="snippet-code-html lang-html prettyprint-override"><div class="title"> hi <div> asd </div> </div>

if want complicate it

class="snippet-code-js lang-js prettyprint-override">var div = document.getelementsbyclassname('title')[0]; //get reference parent div var array = [].map.call(div.childnodes, function(child) { homecoming child.nodetype == 3 && child.nodevalue.trim() ? child.nodevalue.trim() : undefined; }) var name = array.join(); alert(name) class="snippet-code-html lang-html prettyprint-override"><div class="title"> hi <div> asd </div> </div>

javascript jquery html

No comments:

Post a Comment