0

How can i prevent my scroll bar to not move at the top on click event. Visit https://wazoodle01.wsisites.net/knowledge-base/order-information/wholesale-faq.html# and click on any option listed below.You will see the scroll bar goes to the top. How to prevent that. here is my code .

 `<script type="text/javascript">jQuery(document).ready(function(){
jQuery('.faq-main-content').hide();jQuery('.faq-main-content.faq-content').hide();
jQuery(".faq-content-header").delegate('a','click',function(e){
    if(jQuery(e.target).data('link')!=='redirect'){
        jQuery(e.target).toggleClass('question_minus').parent().next('.faq-content').slideToggle();e.preventDefault();
        }});
        jQuery(".faq-header").delegate('a','click',function(e){
            jQuery(e.target).toggleClass('section_minus').parent().next('.faq-main-content').slideToggle();
            if((jQuery(e.target).parent().next('.faq-main-content').children('.faq-content').is(':visible'))){
                jQuery(e.target).parent().next('.faq-main-content').children('.faq-content').slideUp();
                if(jQuery(e.target).parent().next('.faq-main-content').children('.faq-content-header').children('a').is(".question_minus")){
                    jQuery(e.target).parent().next('.faq-main-content').children('.faq-content-header').children('a').removeClass('question_minus');
                    }}

e.preventDefault();
 })});</script>`
1

That is happening because you have href="#", your options are to remove href="#" (valid HTML5) or add the below javascript inside your click event:

e.preventDefault();
5
  • I have already added preventDefault()
    – Nafsss
    Feb 11 '20 at 9:51
  • You should move it higher up so it's the first line inside the click event. Or remove the href.
    – Ben Crook
    Feb 11 '20 at 9:57
  • I would also recommend using .on rather than delegate > .delegate() has been deprecated. It was superseded by the .on() method since jQuery 1.7
    – Ben Crook
    Feb 11 '20 at 9:57
  • check this wazoodle01.wsisites.net/knowledge-base/orders-faq.html# click on any option below. see what happens. On clicking twice the event is triggered
    – Nafsss
    Feb 11 '20 at 10:17
  • Yes, if you remove the href it no longer occurs. Or even better, change the element so it is no longer an <a> tag. If you aren't linking anywhere it doesn't make sense to be an anchor element.
    – Ben Crook
    Feb 11 '20 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.