JQuery: Hide anchor if href is empty

Guy picture Guy · Sep 28, 2011 · Viewed 26.9k times · Source

Been on this one for a while now. Basically, I need to check where a href on an anchor tag with the class .pdf-download is empty, and if it is, hide it.

I have attempted a few options but with no luck. This is what i have so far:

$("a.pdf-download").each(function (i) {
  if ($('[href]:empty',this).length == 1) { 
    $(this).hide();
  } else {
    $(this).show();
  }
});

Answer

amosrivera picture amosrivera · Sep 28, 2011
if ($(this).attr('href') != '') { 
    $(this).hide();
} else {
    $(this).show();
}

Note that you can also do this in css with attribute selectors:

a.pdf-download[href='']{
    display:none;
}

This is not supported in ie6 though.