I have my html structure like
<div>
<div class="comments-title-wr clearFix">
<span id="commentsSize" class="comments-title">Some Text</span>
</div>
<div id="testId">
</div>
</div>
I can retrieve testId
and using this I want to replace text inside <span>
tag.
I tried using
$('#testId').parent().closest('.comments-title').text().replace('something else');
but it's not working
Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content.
Use the textContent property to get the text of a span element, e.g. const text = span. textContent . The textContent property will return the text content of the span and its descendants. If the element is empty, an empty string is returned.
span is an inline-element, while div ist not. you should consider swapping them..
We used the textContent property on the span to change its text content. The textContent property can also be used to read the text content of the element and its descendants. Setting textContent on an element removes all of the element's children and replaces them with a single text node with the provided string.
You probably want
$('#testId').parent().find('.comments-title').text('something else');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With