Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery: Hide anchor if href is empty

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();
  }
});
like image 420
Guy Avatar asked Sep 28 '11 16:09

Guy


1 Answers

I am a jquery beginner myself, but thats how I would do it:

$("a.pdf-download").each(function (i) {

    var aHref = $(this).attr('href');

    if (aHref == '' || !aHref) {

        $(this).hide();

    };

});

Demo: http://jsfiddle.net/BZq9c/1/

like image 85
r0skar Avatar answered Oct 05 '22 05:10

r0skar