Skip to content

Instantly share code, notes, and snippets.

@stephenbelyea
Created March 3, 2016 02:11
Show Gist options
  • Save stephenbelyea/54b7d0aa3e954b29248b to your computer and use it in GitHub Desktop.
Save stephenbelyea/54b7d0aa3e954b29248b to your computer and use it in GitHub Desktop.
Clickable CTAs with sensible link content.
// Manage click/focus states for CTA with inner link.
var setWrapCTAs = function (event) {
var wrap = $('[data-cta-wrap]'),
link = wrap.find('[data-cta-link]');
// Clicking on CTA wrapper triggers internal link.
wrap.on('click', function(e) {
e.preventDefault();
var path = $(this).find('[data-cta-link]').attr('href');
if ( path !== "" ) {
window.location = path;
}
});
// Clicking on internal link avoids bubbling, focus
// and blur toggle .focus helper class on wrapper.
link.on('click', function(e) {
e.stopPropagation();
})
.on('focus', function(e) {
$(this).closest('[data-cta-wrap]').addClass('focus');
})
.on('blur', function(e) {
$(this).closest('[data-cta-wrap]').removeClass('focus');
});
};
// If link wrap CTAs, prep links.
if ( $('[data-cta-wrap] [data-cta-link]').length ) {
setWrapCTAs();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment