Learning
Learning

Reputation: 20001

Remove parent anchor element if span has only # in it

I want to remove all anchor element where nested <span></span> tag has only # in it example

Remove This one

<a  href="/search.aspx?search=something" class="keyword"><span class="tags">#</span></a>

Don't remove this one has some text with #

<a  href="/search.aspx?search=something" class="keyword"><span class="tags">#Sometext</span></a>

Upvotes: 0

Views: 113

Answers (4)

Shaunak D
Shaunak D

Reputation: 20636

Use filter() function,

$('a').filter(function(){
    return $.trim(this.text) === '#';
}).remove();

Or

$('a').filter(function () {
    return $.trim($('span.tags',this).text()) === "#";
}).remove();

Upvotes: 1

Zafar Ahmad
Zafar Ahmad

Reputation: 428

Try this, if there is one or more than one .tags span, it should work

<script>
$(function(){  
    $('.tags').each(function(){
        if( $(this).text() == "#" ){
            $(this).parent().remove();
        }
    });
});
</script>

Upvotes: 1

K K
K K

Reputation: 18099

Try this:

$(".keyword").filter(function () {
    return $.trim($(this).text()) == "#" ? true : false;
}).remove();

Demo: http://jsfiddle.net/GCu2D/663/

Upvotes: 1

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

You can use filter to return the spans which has only # text in it. Then find the parent anchor either by closest("a") or parent()

$("span.tags").filter(function() {
    return $(this).text().trim() == "#"
}).closest("a").remove();

Upvotes: 1

Related Questions