0

I have the following link which i want to modify its text from "new subsite" to "create new customer site":-

enter image description here

so i wrote the following jquery :-

$('a#createnewsite').text('Create New Customer Site');

which replaced not only the text but also the span that contain the "+" image, while i was trying to keep everything except replacing the text.. here is the result after applying the jQuery :-

enter image description here

Salman A
  • 248,760
  • 80
  • 417
  • 510
john Gu
  • 7,691
  • 60
  • 207
  • 407
  • [How to select text nodes with jQuery](http://stackoverflow.com/questions/298750/how-do-i-select-text-nodes-with-jquery) – A1rPun Jun 12 '15 at 14:30

2 Answers2

1

You need to select the text node, then change the text of it.

$('#createnewsite')
    .contents()
    .filter(function() { return this.nodeType === Node.TEXT_NODE; })
    .text('Create New Customer Site');
A1rPun
  • 15,440
  • 6
  • 55
  • 87
1

Text nodes can be selected by using jQuery.contents together with jQuery.filter:

$(function() {
  $("a#createnewsite").contents().filter(function() {
    // filter text nodes that contain one or more non-whitespace characters
    return this.nodeType === Node.TEXT_NODE && /\S/.test(this.nodeValue);
  }).replaceWith("new text");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<a id="createnewsite" href="#">
  <span>
    <img src="http://dummyimage.com/64x64/000/fff" width="64" height="64">
  </span>
  new subsite
</a>
Salman A
  • 248,760
  • 80
  • 417
  • 510