1

I have a checkout page on a site that generates a shipping message. I need to change the text of that shipping message.

Here is the path to the element needing changed:

#order_review > table > tfoot > tr.shipping > td > p

Here is the approach I'd like to take (correct me if a better one), but I need help drilling down to the right element:

var oldstring = "Enter your shipping information above to show shipping options."
var changeit = document.getElementById("order_review");
changeit.oldstring.replace("Enter your shipping information above to show shipping options.", "Enter your full address to verify free delivery.");
3
  • 1
    document.querySelector("#order_review > table > tfoot > tr.shipping > td > p") Commented Feb 21, 2018 at 16:05
  • 1
    Strings are immutable. replace returns a new string. And you are may looking for the textContent property Commented Feb 21, 2018 at 16:06
  • 1
    @JonasW. more than that, the OP is trying to "update" the element itself. Commented Feb 21, 2018 at 16:07

3 Answers 3

1

You can use the querySelector method as Andereas mentioned in the comments. You don't need to use replace, simply assign the new text to innerHTML or textContent:

var changeit = document.querySelector("#order_review > table > tfoot > tr.shipping > td > p");
changeit.textContent = "Enter your full address to verify free delivery.";

If you want to keep the reference to the order_review and drill down from there, you can do this:

var changeit = document.getElementById("order_review");
changeit.querySelector("table > tfoot > tr.shipping > td > p").textContent = "Enter your full address to verify free delivery.";
Sign up to request clarification or add additional context in comments.

Comments

1

I like to use textContent when updating text on the DOM:

let el = document.querySelector("#order_review > table > tfoot > tr.shipping > td > p");
el.textContent = "Enter your full address to verify free delivery.";

1 Comment

Improved answer.
1

var qs = document.getElementById("order_review");
qs.querySelector("table > tfoot > tr.shipping > td > p").innerHTML = "Enter your full address to verify free delivery.";
<div id="order_review">
  <table>
    <tfoot>
      <tr class="shipping">
        <td>
          <p>Enter your shipping information above to show shipping options.</p>
        </td>
      </tr>
    </tfoot>
  </table>
</div>

1 Comment

Question's title: Change a String of text from generated code in javascript.

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.