I have the following HTML snippet:
<span class="target">Change me <a class="changeme" href="#">now</a></span>
I would like to change the text of the node (i.e. “Change me”) within the range from jQuery, leaving the nested <a> tag with all attributes, etc. intact. My initial huch was to use .text(...) on the span node, but as it exits , it will replace the entire inside with the passed text content.
I solved this by first cloning the <a> tag, and then installing the new <span> text content (which will remove the original <a> tag) and finally adding the cloned <a> tag to my <span> . It works, but feels overkill for a simple task like this. Btw. I can not guarantee that inside the span there will be the initial text node - it may be empty, like:
<span class="target"><a class="changeme" href="#">now</a></span>
I did jsfiddle . So what would be a neat way to do this?
source share