So if I had
<div id="outside">
Hello
<div id="inside">
Qwertyuiop
</div>
</div>
How would I get the InnerHTML of the outside without including anything from the inside or any other HTML tags? (bascially how to get "Hello")
So if I had
<div id="outside">
Hello
<div id="inside">
Qwertyuiop
</div>
</div>
How would I get the InnerHTML of the outside without including anything from the inside or any other HTML tags? (bascially how to get "Hello")
This is not a serious answer, and is based on Darin Morris'
highly destructive answer but is 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
$('#outside')[0].firstChild.nodeValue
but who knows... This technique may come in handy for someone at some point :)
Another option:
$(function(){
var theHtml = $("#outside");
var texto = theHtml[0].childNodes[0].data;
alert((texto));
});
Fiddle:
$(function() {
var theHtml = $("#outside");
var texto = theHtml[0].childNodes[0].data;
alert((texto));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="outside">
Hello
<div id="inside">
Qwertyuiop
</div>
</div>
<div id="test"></div>
theHtml[0].firstChild.nodeValue
is shorter/faster. –
Padraic Try to use .contents()
at this context
alert($("#outside").contents().first()[0].nodeValue);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="outside">
Hello
<div id="inside">
Qwertyuiop
</div>
</div>
first()[0]
is redundant. $("#outside").contents()[0].nodeValue
does the same thing :) –
Padraic You are trying to get the value of the first child so
alert($('#outside').prop('firstChild').nodeValue)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="outside">
Hello
<div id="inside">
Qwertyuiop
</div>
</div>
prop
for HTML element properties (rather than just $('#outside')[0].firstChild.nodeValue)
? –
Padraic .prop()
will need to run some jQuery code again –
Haemolysin prop
then? (either will break if there is no child, so jQuery prop
is not really adding any value). +1 anyway :) –
Padraic Array.prototype.filter.call(document.querySelector('#outside').childNodes, function (node) {
return node.nodeValue;
});
You can also try this in Vanilla, it would also handle a case if you had other strings after the div.
There probably is 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
for
loop and if
check as children
does not return elements of nodeType
3. You get the same effect with $("#outside").children().remove();
jsfiddle.net/TrueBlueAussie/ez4v83v5/5 Basically it is not only DOM-destructive, but also inefficient too. –
Padraic try the jquery api children, try the fiddle
$(document).ready(function(){
alert($('#outside').clone() //clone the element
.children() //select all the children
.remove() //remove all the children
.end() //again go back to selected element
.text());
});
hope it helps...
Hello
(which is a text node, invisible to children()
), not Qwertyuiop
. Sorry. –
Padraic © 2022 - 2024 — McMap. All rights reserved.