Get an InnerHTML element that is not inside another tag
7 answers
1 Interesting option:
This is not a serious answer and is based on Darin Morris' very destructive answer, but slightly less destructive:
// Clone the element var $clone = $("#outside").clone(); // Remove all the children (leaves text nodes) $clone.children().remove(); alert($clone.text()); http://jsfiddle.net/TrueBlueAussie/ez4v83v5/4/
Again I would not recommend this as an alternative to say
2 My serious answer:
$('#outside')[0].firstChild.nodeValue but who knows ... This technique may come in handy to someone at some point :)
+4
Another option:
$(function(){ var theHtml = $("#outside"); var texto = theHtml[0].childNodes[0].data; alert((texto)); }); Working fiddle: http://jsfiddle.net/robertrozas/vhdbj1ck/1/
+3
There is probably a better way to do this, but this should work for you:
var kids = $("#outside").children(); for(var i = 0; i < kids.length; i++) { if(kids[i].nodeType != 3) { $(kids[i]).remove(); } } alert($("#outside").text()); See this JSFiddle: http://jsfiddle.net/ez4v83v5/1
0