Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.text() set modifies more than get

Tags:

jquery

Consider the following HTML, I want to change 'blah' to something else.

<span> 
  <img src="/i/f_spacer.gif" class="my_class"> 
  blah
</span>

Considering $('.my_class').parent().text() returns 'blah' and .html() returns everything - I thought something like this would work

$('.my_class').parent().text('foo');

but it turns it into

<span> 
  foo
</span>

Do I need to include a span around 'blah', or can I use some sort of pseudo-class to identify this text?

Cheers

like image 698
Scott Avatar asked Jan 11 '23 04:01

Scott


1 Answers

Simply use nextSibling.data

$('.my_class')[0].nextSibling.data = "foo";

DEMO

like image 85
Sudharsan S Avatar answered Feb 27 '23 16:02

Sudharsan S