Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

want to replace the text inside span of parent div

Tags:

jquery

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

like image 217
Harry Avatar asked Apr 15 '13 15:04

Harry


People also ask

How do I change text in a div element?

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.

How would you get the text written inside a span element?

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.

Can we use span tag inside div tag?

span is an inline-element, while div ist not. you should consider swapping them..

Does span have textContent?

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.


1 Answers

You probably want

$('#testId').parent().find('.comments-title').text('something else');
like image 56
Denys Séguret Avatar answered Oct 21 '22 07:10

Denys Séguret