Horen
Horen

Reputation: 11382

jQuery selector for link without an image inside

I bind a click function to a link:

$("body").on("click", "a", function() {
    //do something
}); 

Now I am looking for a selector that will only match if the link does not contain an image tag. So kind of similar to a:not(img) but img being the child element.

How can I do that?

Upvotes: 10

Views: 961

Answers (3)

Adil
Adil

Reputation: 148140

You can use not with has to filter anchors not having img

Live Demo

$("body").on("click", "a:not(a:has(img))", function() {
    //do something
    alert("");
}); 

Upvotes: 1

Eli
Eli

Reputation: 14827

Try this:

$("body").on("click", "a:not(a:has(img))", function() {
    //do whatever you want here
}); 

Upvotes: 12

karacas
karacas

Reputation: 2132

Try this

$('a:not(:has(>img))').click(function(){
    alert('click');
})​;

Upvotes: 1

Related Questions