0

I'm piggy backing off of a previous post that talks specifically about removing a string from inside an element. The code to do that is:

  $this.text( $this.text().replace("text to strip out", "") );

I've successfully stripped out the text "categories: "

FYI: This is a blog page with categories that are dynamically added. HTML structure before I strip out "categories: ":

<container>
   <div class="categories">
     "categories: "  //This is what I strip out with above code
     <a href = "...">Category Name</a> //This is a linked category name
   </div>
</container>

The problem is that when I remove "categories: " it is also stripping out the and leaving me with an unlinked "Category Name"

After the jquery runs html looks like this:

 <container>
   <div class="categories">Category Name</div>
</container>

I need to keep the link active, as I want the ability for the user to click "Category Name" to stay.

1
  • Does the code you use for the replace include anything about the link? Commented Feb 7, 2013 at 17:06

3 Answers 3

0

To remove a textNode you need to use contents() to retrieve them, then a combination of filter() and remove(), like this:

$('.categories')
    .contents()
    .filter(function() { return this.nodeType == 3; })
    .remove();

Example fiddle

Note this will remove all direct child textNodes. If you want to filter by the text of the node, amend the filter() function accordingly.

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

Comments

0

One way to do it, don't know if it's the best way, is to save the anchor element as a javascript variable. Then after you remove the stuff you want to remove, inject that anchor back in.

Comments

0

Try this instead...

$("div.categories").each(function() {
    var $this = $(this);
    $this.html($this.html().replace("categories: ", ""));
});

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.