I am just learning how to use jquery correctly and I cannot seem to figure out why this approach doesn't stop the anchor from redirecting.
Basically, I want to check if an image has a certain data tag in its anchor and when clicked it stops the link and does something (basically making my own light box).
html
<ul class="imageList imgListIntro">
<li>
<a href="img/cc/intro-1.jpg"
data-ocular="test-1"
data-title="Test"
class="hvr-wobble-vertical">
<img src="img/cc/intro-1.jpg" class="" />
</a>
</li>
</ul>
js
if ($('img a').attr('data-ocular')){
$('img a').click(function(event) {
event.preventDefault();
alert('Ocular Tag');
});
}
<img />elements should never contain other elements (it's an empty element, and self closing), so the selectorimg adoesn't make sense from the get go. Most of the answers seem to have missed this.