Want to replace text inside span of parent div

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 get testId, and using this, I want to replace the text inside the tag <span>.

I tried to use

  $('#testId').parent().closest('.comments-title').text().replace('something else');

but it does not work

+5
source share
5 answers

You probably want

$('#testId').parent().find('.comments-title').text('something else');
+2
source

Try it,

Live demo

$('#testId').prev('.comments-title-wr.clearFix').find('.comments-title').text('something else');
+2
source
$('#testId').prev().find('span').text('HI');
+2
source
$('#testId').prev().children('span').text('something else');
+1
source

You can use siblings. http://api.jquery.com/siblings/

.comments-title-wrand #testIdare brothers and sisters. Somehow I skipped this internal interval, the answer was updated.

$('#testId').siblings('.comments-title-wr').find('.comments-title').text('something else');
0
source