I have some List items, which contain a div, which contains a link. I’d like to remove this Link from all the divs. This is my current function:
jQuery('.post_meta').each(function(){
var el = jQuery('.post_meta a');
el.html(el.html().replace(/, Slider/ig, ""));
});
This function dupolicates one div and add its contents to all the others. What would be the right way?
This is my markup:
<ul>
<li>
<div class="post_meta">
<a href="#">Anything</a>, <a href="#">Slider</a>, <a href="#">Anything2</a>
</div>
</li>
</ul>
Seems you want something like this:
This takes all links inside
.post_metaelements and filters them by their content (whether the text isSlider. It then removes those elements.Reference:
filter,trim,removeUpdate: Regarding your comment. Removing the comma is a bit more tricky, but you can do it like this (after you removed the links):
It is probably not very performant though…
Update 2: Apparently removing the comma did not work because after you removed the element node, you have two adjacent text nodes and not one.
This works though:
DEMO
This basically removes duplicate adjacent text nodes.
Why am I doing this so complicated?
Because it looks there might be JavaScript event handlers attached to the links and if you get and set the HTML, these handlers will be lost.
If this is not the case and the HTML is really like this, you could do:
DEMO 2
But as said, this only works if (a) the HTML matches exactly with the string and (b) you don’t bind any event handlers to the links (they will be lost).