Change the text in <h2> without changing the span of the same h2 tag
4 answers
jsFiddle: https://jsfiddle.net/CanvasCode/y6p6xzfx/
$(function () {
$span = $('h2').find('span')
$('h2').text("Hello");
$('h2').append($span);
});
Take a copy of your range, refresh the h2 text, and then add the span back to the h2 tag.
I forgot the filter using the c1
div class . here is the updated code
jsFiddle https://jsfiddle.net/CanvasCode/y6p6xzfx/1/
$(function () {
$div = $('.c1');
$h2 = $div.find('h2');
$span = $h2.find('span')
$h2.text("Hello");
$h2.append($span);
});
. , .
+4
nodeValue
textNode
.
querySelector()
- domfirstChild
- firstNode, textNodenodeValue
- textNode
document.querySelector('.c1 h2').firstChild.nodeValue = 'hi'
<div class="c1">
<h2>here<span class="span1">X</span></h2>
</div>
UPDATE: jQuery contents()
replaceWith()
$('.c1 h2').contents().first().replaceWith('hi');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="c1">
<h2>here<span class="span1">X</span></h2>
</div>
+3