Cannot read property "top" appears when button "button" opens modality window

The following code works on page load. However, when I click on a button that should open a modal window, I get the following error:

Uncaught TypeError: Cannot read property 'top' of undefined

How can I fix this error?

if ($(window).width() > 992) {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            $('html, body').stop().animate({
                scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 110
    });
    // alert("large");
}
else {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            $('html, body').stop().animate({
                scrollTop: $(hash).offset().top - 50}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 70
    });
    // alert("small");
}   

//modal popup function
function popup_modal(item){
    var link = $(item).attr('id');
    $('#bootstrap_modal').load('/'+link+'');
    $('#bootstrap_modal').modal('show'); 
    $("#bootstrap_modal").on("show", function () {
        $("body").addClass("modal-open");
    }).on("hidden", function () {
        $("body").removeClass("modal-open")
    });
}

//Modal pop up
$('.mmodal').on('click', function(){
    popup_modal(this);
});

      

+3


source to share


2 answers


Likely reason you are getting the error that the element $(hash)

does not exist for your modal button case. And the modal button is the element that gets into the event $(".page-scroll a[href^='#'], #intro a").on('click')

. if there is no element with an ID equal to the "href" attribute of the button pressed, you cannot get its "offset.top". Place " console.log(hash)

" to check what you get there.

A Possible solution:



if ($(hash).length) {
    $('html, body').stop().animate({
         scrollTop: $(hash).offset().top - 100}, 2000, 'easeOutExpo');
}

      

+1


source


Try the code below:

if ($(window).width() > 992) {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            var newVal = $(hash).offset().top - 100;
            $('html, body').stop().animate({
                scrollTop: newVal}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 110
    });
    // alert("large");
}
else {

    (function($) {
        "use strict";
        $(".page-scroll a[href^='#'], #intro a").on('click', function(e) {
            e.preventDefault();
            var hash = this.hash;
            var newVal = $(hash).offset().top - 50;
            $('html, body').stop().animate({
                scrollTop: newVal}, 2000, 'easeOutExpo');
        });
    })(jQuery);

    $('body').scrollspy({
        target: '.navbar',
        offset: 70
    });
    // alert("small");
}   

//modal popup function
function popup_modal(item){
    var link = $(item).attr('id');
    $('#bootstrap_modal').load('/'+link+'');
    $('#bootstrap_modal').modal('show'); 
    $("#bootstrap_modal").on("show", function () {
        $("body").addClass("modal-open");
    }).on("hidden", function () {
        $("body").removeClass("modal-open")
    });
}

//Modal pop up
$('.mmodal').on('click', function(){
    popup_modal(this);
});

      



I have set a variable newVal

to value$(hash).offset().top - 50

0


source







All Articles