Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 903
Horen Avatar asked Dec 22 '12 17:12

Horen


3 Answers

Try this:

$("body").on("click", "a:not(a:has(img))", function() {
    //do whatever you want here
}); 
like image 91
Eli Avatar answered Oct 21 '22 22:10

Eli


Try this

$('a:not(:has(>img))').click(function(){
    alert('click');
})​;
like image 29
karacas Avatar answered Oct 22 '22 00:10

karacas


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("");
}); 

like image 20
Adil Avatar answered Oct 21 '22 23:10

Adil