Given the basic structure, how can I turn a series of divs into links without turning each div into a link? Here is an example:
<div class="boxes">
<div class="box"><p>Some text with a <a href="https://www.google.com">link</a></p></div>
<div class="box"><p>Some text without a link</p></div>
<div class="box"><p>Some text with a <a href="https://www.example.com">link</a></p></div>
<div class="box"><p>Some text without a link</p></div>
</div>
And the related jQuery that I use to make divs interactive:
$(document).ready(function() {
if($('.boxes p a').length){
$(".boxes .box").click(function() {
window.open($(this).find("a").attr("href"));
return false;
});
}
});
The problem I'm encountering is that the click function applies to all divs, not just links with links.
The desired behavior is only to create a fully interactive div only when an anchor element is detected.
For the purpose of this use, div ( .box
) is dynamically generated and wrapping the element in the anchor ( <a href="#"><div> </div></a>
) tag is not possible.
Fiddle: https://jsfiddle.net/fu8xLg0d/
source
share