Dee
Dee

Reputation: 3255

Jquery exclude element that not have <a href="#"> inside

how can I exclude from this selection element that not have a href="#" inside of it? I want my stuff is executed only for .leading that have a href="#"... thank you

//js
$(".leading").hover(function(){
// do stuff
}

//DOM
<div class=".leading"><div ><a href="#"> <img src="img1.jpg" /> jquery affected </a></div></div>
<div class=".leading"><div ><img src="img2.jpg" /> jquery not affected </div></div>

Upvotes: 2

Views: 469

Answers (2)

Orbling
Orbling

Reputation: 20602

Something like this I believe:

$('.leading:has(a[href=#])');

Note, your classes should not have a dot . prefix in the HTML itself.

Upvotes: 2

cfeduke
cfeduke

Reputation: 23236

$("div.leading div a[href='#']")

... will select only those anchor tags with a href attribute set to the value #.

You can then use .parent().parent() to walk up to the div.leading element.

Also in your example HTML you are using .leading as a class name which is invalid, it should be leading.

Upvotes: 0

Related Questions