I explain what I would like to achieve with an image, I think it is easier.
What I would like to do (click to see the example)
I’ve tried this but it doesn’t work
$('.entry-tpl-grid').each(function(){
$(this).on('click', function(){
location.href = $(this).find('div > header > h3> a').attr('href');
});
});
Where I want to apply it (click to see the example)
<article class="entry-tpl-grid">
<figure class="entry-featured-media">
<a href="#" class="g1-frame">
<div class="g1-frame-inner">
<img src="https://--.com/300x300.png">
</div>
</a>
</figure>
<div class="entry-body">
<header class="entry-header">
<h3 class="entry-title">
<a href="https://www.--.com/" class="woocommerce-LoopProduct-link"> Product name </a>
</h3>
</header>
</div>
</article>
Thanks in advance.
3
Answers
You can use
[0]
to access the DOM Element directly, and then use the standard JS.click()
using the each statment you would be adding multiple event handlers to each child element so would it not just be easier to add the event handler on the parent and if a child element is clicked it runs the parents event …
This answer uses vanilla js: