CMS Development

suniltodkar
Participant

event.preventDefalut(); does not working on a tag

SOLVE

In my webpage there is CTA href link but i dont want to redirect that link. I want to scroll to page section. So my question is that i used below code but does not working:
$(document).ready(function(){
$("#cta_button_3852769_81fc24f7-1d1b-4cf0-aea6-b420d199a42b").click(function(event){
event.preventDefault();
});
});

0 Upvotes
1 Accepted solution
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

event.preventDefalut(); does not working on a tag

SOLVE

@suniltodkar - CTA's load in dynamically so, you are probably executing that code before the button is available. You should attach the listener to the document or add an interval that checks for the existance of the CTA.

I answered a similar question here: https://community.hubspot.com/t5/Content-Design-Questions/CTA-does-not-take-anchor-for-rich-text/m-p...

$(document).on("click", '.hs_cos_wrapper_type_cta a', function(e) {
  var thisE = $(this);
  var linkto = thisE.attr('cta_dest_link');
  if (linkto.indexOf('#') > -1) {
     var hashid = linkto.split("#").pop();
     $('html, body').animate({ scrollTop: $('#'+hashid).offset().top}, 1000);
     e.preventDefault();
  }

});

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.

View solution in original post

1 Reply 1
tjoyce
Solution
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

event.preventDefalut(); does not working on a tag

SOLVE

@suniltodkar - CTA's load in dynamically so, you are probably executing that code before the button is available. You should attach the listener to the document or add an interval that checks for the existance of the CTA.

I answered a similar question here: https://community.hubspot.com/t5/Content-Design-Questions/CTA-does-not-take-anchor-for-rich-text/m-p...

$(document).on("click", '.hs_cos_wrapper_type_cta a', function(e) {
  var thisE = $(this);
  var linkto = thisE.attr('cta_dest_link');
  if (linkto.indexOf('#') > -1) {
     var hashid = linkto.split("#").pop();
     $('html, body').animate({ scrollTop: $('#'+hashid).offset().top}, 1000);
     e.preventDefault();
  }

});

If this answer helped, please, mark as solved 😄


tim@belch.io | forms.belch.io | Design your own Beautiful HubSpot Forms; No coding necessary.

 

Drop by and say Hi to me on slack.