Robin Alexander
Robin Alexander

Reputation: 1004

Links do not respond to click event

After adding the following script, the links (like <a href="http://www.example.com">example.com</a> stopped working. They do not respond to click events anymore.

Could somebody please help me to solve this problem?

$(document).ready(
function() {
    $(".show").click(function() {
        $(".show").hide();
        $(".hide").show();
        $("#cpks-table").fadeIn();
    });
});

$(document).ready(
function() {
     $(".hide").click(function() {
        $(".show").show();
        $(".hide").hide();
        $("#cpks-table").fadeOut();
    });

    $(".hide").click(function(){
    $('html, body').animate({scrollTop : 0},800);
    return false;
    });

    $( ".tggl" ).click(function() {
         $(".show").fadeToggle();
        $(".hide").fadeToggle();
    $( "#cpks-table" ).fadeToggle();
    }); 

$(document).on('click', 'a', function(event){
event.preventDefault();

$('html, body').animate({
    scrollTop: $( $.attr(this, 'href') ).offset().top-100
}, 500);
});

});

Upvotes: 1

Views: 39

Answers (2)

tanaydin
tanaydin

Reputation: 5296

Hello you are preventing default behavior of links by

event.preventDefault();

line. After your animation is done, you should open link by Javascript like,

window.open($.attr(this, 'href'))

So you should write something like

$(document).on('click', 'a', function(event){
    event.preventDefault(); //preventing default behavior of link click
    //do what you need
    var self = this;
    $('html, body').animate({
        scrollTop: $( $.attr(self, 'href') ).offset().top-100
    }, 500, function() {
        //redirect user where intended
        window.location.href = $.attr(self, 'href');
    });
});

Upvotes: 1

RohitS
RohitS

Reputation: 1046

You need some more line as:

 $(document).on('click', 'a', function(event){

    event.preventDefault(); //preventing default behavior of link click
    //do what you need
    $('html, body').animate({
        scrollTop: $( $.attr(this, 'href') ).offset().top-100
    }, 500);
   //redirect user where intended
    window.location.href=$(this).attr('href');

    });

Upvotes: 0

Related Questions