Harry
Harry

Reputation: 4773

want to replace the 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 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

Upvotes: 5

Views: 8236

Answers (5)

Jack
Jack

Reputation: 8941

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

.comments-title-wr and #testId are siblings. Somehow missed that inner span, answer updated.

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

Upvotes: 0

Roko C. Buljan
Roko C. Buljan

Reputation: 206121

$('#testId').prev().find('span').text('HI');

Upvotes: 2

Adil
Adil

Reputation: 148120

Try this,

Live Demo

$('#testId').prev('.comments-title-wr.clearFix').find('.comments-title').text('something else');

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

$('#testId').prev().children('span').text('something else');

Upvotes: 1

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

You probably want

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

Upvotes: 2

Related Questions