user123
user123

Reputation: 409

Hide div with javascript when no link

I have a website into modx cms, I'm trying to remove or hide a div when into that div there is no tag. How can I do this?

I tried this but no luck:

jQuery(function($) {
if ($(".pages a")) {$(".pages").remove();}
});

< div class="pages">[+previous+] [+pages+] [+next+]< /div> 

Upvotes: 0

Views: 134

Answers (4)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167250

Hide it this way, so that you can show them up when there are links:

if ($(".pages a").length == 0) {
    $(".pages").hide();
}

And when the links are there, or you making an AJAX call, do this:

$(".pages").show();

Upvotes: 0

Pieter Willaert
Pieter Willaert

Reputation: 879

another shorter answer would be

$('.pages:not(:has(>a))').css("display", "none");

click to see...

reference jQuery.not()

Upvotes: 1

Akhil Sekharan
Akhil Sekharan

Reputation: 12693

I'm not sure if this is what you want:

$(function($) {
    $(".pages").each(function(){
        if(!$(this).find('a').length)
            $(this).remove();
    }); 
});

Upvotes: 0

boz
boz

Reputation: 4907

If you are trying to check if the <a> tag exists inside the div then you could try:

if($(".pages a").length == 0) {
    // links don't exist
    $(".pages").remove();
} else {
    // links exist
}

Upvotes: 1

Related Questions