skip to Main Content

I’m trying to remove the text (in this example, it’s "Hi – "). But it’s not working.

<div class="custom-card-title">
  <!-- Change to 1-1/32 HELLO  -->
  <a href="#">Hi - 1-1/32 HELLO</a>
</div>
let name = $('.custom-card-title a').text();
const newName = name.split(" - ").pop();
name = `${newName}`

3

Answers


  1. let name = $('.custom-card-title a').text();
    const newName = name.split("Hi - ")[1];
    name = `${newName}`
    
    Login or Signup to reply.
  2. You have to use .html() function to append the modified text to the anchor tag.

    let name = $('.custom-card-title a');
    const newName = name.text().split(" - ").pop();
    name.html(newName);
    
    Login or Signup to reply.
  3. You can use .html() or .text() here to set the contents of an anchor element.

    let name = $('.custom-card-title a').text();
    const newName = name.split("-").pop();
    name.text(newName) // or name.html(newName)
    

    html() is used to return or change the inner html and text content of selected elements. text() is used to return or change the text content of selected elements.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search