0

I am trying to replace text within an element on the fly, however .text() function is replacing both text and HTML. Any thoughts on how I only edit the actual text?

<div>
  <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a>
<div>

I want to change the words "My Title" to "My New Title", however whenever I use .text() function, it replaces all of my html and my anchor tag disappears.

Here is my jQuery:

$('h3.titleHeading').text("My New Title")

My output is then - again, the code removes my anchor tag which I need for a tooltip.

<div>
  <h3 class="titleHeading">My New Title 
<div>
3
  • 8
    wrap your text in a span. that way its specific to exactly what you want. Commented Jun 12, 2019 at 15:49
  • 5
    Note that the h3 element should be closed, perhaps that's why you're confused by what is happening. <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a> should be <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a></h3> and .text(...) is replacing everything inside the <h3>...</h3>. Commented Jun 12, 2019 at 15:53
  • 2
    @Stijn or, possibly, <h3 class="titleHeading">My Title</h3><a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a> Commented Jun 12, 2019 at 15:55

4 Answers 4

2

If you do not have access to the code, loop through only the text nodes and replace their textContent with your new content. This will keep the other existing HTML intact.

Taken from this answer.

$('h3.titleHeading').contents().filter(function() {
    return this.nodeType == 3
}).each(function(){
    this.textContent = this.textContent.replace('My Title','My New Title');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a>
<div>

Sign up to request clarification or add additional context in comments.

Comments

2

When developing HTML, if you have an element with child HTML elements, it is a bad idea to have it contain plain-text as well. This causes problems like what you see here. An easy solution is to put your text inside a span. (Note: Don't forget to close your h3.)

<div>
  <h3 class="titleHeading">
    <span id="titleHeadingText">My Title</span>
    <a ng-click="openTooltip('getMoreInfo')">
      <span class="toolTip"></span>
    </a>
  </h3>
<div>

You can change the elements of the text easily, now, without affecting the other elements.

$("#titleHeadingText").text("My New Title");

1 Comment

that makes sense, however the original developer did not do that and I am trying to alter his code using dom manipulation because I don't have access to the code base. Any thoughts on how I can accomplish this without access to the code base?
1

You should do a few things as outlined in the code below

  1. Close the <h3> element
  2. Wrap the text you want to change in a span

Example

<div>
  <h3 class="titleHeading">
    <span class="titleHeadingText"> My Title</span>
    <a ng-click="openTooltip('getMoreInfo')">
      <span class="toolTip"></span>
    </a>
  </h3> 

jQuery

$('h3.titleHeadingText').text("My New Title")

1 Comment

that makes sense, however the original developer did not do that and I am trying to alter his code using dom manipulation because I don't have access to the code base. Any thoughts on how I can accomplish this without access to the code base?
1

To achieve expected result, just update innerText for h3 by contents() data instead of using text()

  1. Jquery contents() will return object of DOM elements
  2. 'data' property will provide the innerText of element

$('.titleHeading').contents()[0].data = "My New Title"
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div>
  <h3 class="titleHeading">My Title <a ng-click="openTooltip('getMoreInfo')"><span class="toolTip"></span></a>
<div>

codepen - https://codepen.io/nagasai/pen/YoXXjd?editors=1010

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.