I am talking about a span arranged this way -
<span class="x">
<span class="y"></span> my text
</span>
Is there a way to change this text without wrapping it inside another tag?
I am talking about a span arranged this way -
<span class="x">
<span class="y"></span> my text
</span>
Is there a way to change this text without wrapping it inside another tag?
Something like this :
document.getElementsByClassName('y')[0].nextSibling.nodeValue = 'some other text';
Your question has been asked and answered before. Check this.
The JQuery solution to your question is :
$('.x').get(0).lastChild.nodeValue = " some-text-2";
.
But anyhow, here's the full answer:
HTML
<div class='x'>
<span class='y'></span>
some-text-1
JS
$('.x').get(0).lastChild.nodeValue = " some-text-2";
If you want to change a comment using jQuery, you will need to use .contents()
and look at the node-type 8 for comment.
This is one more Jquery based answer apart from the native javascript solution from adeneo.
$(".x").contents().filter(function() {
return this.nodeType == 3;
}).text('blah');