0

I have a span with id similar to:

<span id="xxx" clas="some_classes">
   <span>This text is fixed</span>
   This text can be changed
</span>

I need to change the text "This text can be changed" in another text using jquery.

I can't use $("#xxx").text("My new text"); because the nested span need to stay here.

How can replace only text?

Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33
  • why not use a span with a class to specify which text can be changed ? – Dylan KAS Nov 05 '19 at 11:58
  • @DylanKas the code is generated by external plugin. i can't chose how to build it – Giuseppe Lodi Rizzini Nov 05 '19 at 11:59
  • 1
    You can use .contents() and .filters() to filter on a specified nodeType, In your case : $('#xxx').contents().filter(function() { return this.nodeType == 3 }).each(function(){ this.textContent = this.textContent.replace('My new Text'); }); – Dylan KAS Nov 05 '19 at 12:01

0 Answers0