Skip to content

Instantly share code, notes, and snippets.

@rutger1140
Created January 29, 2016 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rutger1140/4282a6449de15665d299 to your computer and use it in GitHub Desktop.
Save rutger1140/4282a6449de15665d299 to your computer and use it in GitHub Desktop.
Simple javascript helper to find link element in block and follow link on click
function clickableBlock() {
// Each element with data-clickable is searched for an link and onclick
// the link is followed
$("[data-clickable]").each(function(){
var $this = $(this),
link = $this.find("a").attr("href");
if(link) {
$this.click(function(){
window.location = link;
});
}
});
}
@rutger1140
Copy link
Author

Usage:

<div class="block" data-clickable>
  <h2>Random other stuff</h2>
  <p>Text as well. This entire block is clickable</p>
  <a href="yes.html">Follow me</a>
</div>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment