John Boe
John Boe

Reputation: 3621

How to get collection of elements which has not attribute?

How to get collection of elements which has not attribute with JQuery?

I want to find links which contain image which has not alt attribute.

Should I use something like this? $('a>img[alt!=""]')

Edit:

I have found a work around it. There is a method .has which can detect if there is a child tag. But in my project I am deriving the selector from a string which is split. That would lead to problems using .has.

var selectors = 'a.mark-video;a.sp5;a>img[alt!=""]'
selectors = selectors.split(";").join(", ");
$(selectors).each(
function(){
   var current = this;
   if (this.nodeName == "IMG")
     {
     current = this.parentNode;
     if (current.nodeName != "A")
       current = this.parentNode;
     if (current.nodeName != "A")
       return false;
     current = $(current);
     }
  }
);

In the function I look to the parent if it is a. I used the "a>img" selector in different way than it is expected in JQuery but this is in the project where I try to search for links not images so it makes sense that I am not looking for images...

Upvotes: 0

Views: 31

Answers (1)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28523

Try this : Use :not with attribute name in square bracket, see below code

$('a > img:not[alt]')

Note: put one space before and after of >

Upvotes: 1

Related Questions