Uncaught TypeError: Cannot read property ‘nodeName’ of undefined magento2

Whenever we use Magento2 modal popup we may get below JS issue some times. This is might be because of the script is rendering before html.We can also restrict with checking some additional condition. As like below. Below is one of the condition, similarly we can validate other way also.
Console issue : Uncaught TypeError: Cannot read property ‘nodeName’ of undefined magento2
Solution for JS issue:

Check length of your div whether you declared div is available or not.
if (jQuery("#mymodalpopup").length != 0) { 
var pp = modal(options, $('#mymodalpopup')); 
jQuery('#mymodalpopup').modal('openModal');
}

Post by : Senthil.J

Smoking Injurious to Health !

4 thoughts on “Uncaught TypeError: Cannot read property ‘nodeName’ of undefined magento2”

  1. Gracias MrJute!!! funciona fantástico:

    Before:
    require([
    ‘jquery’,
    ‘Magento_Ui/js/modal/modal’
    ],
    function($,modal) {
    var options = {
    type: ‘popup’,
    responsive: true,
    innerScroll: true,
    title: ”,
    buttons: [{
    text: $.mage.__(‘Close’),
    class: ”,
    click: function () {
    this.closeModal();
    }
    }]
    };

    var popup = modal(options, $(‘#header-mpdal’));
    $(“#click-header”).on(‘click’,function(){
    $(“#header-mpdal”).modal(“openModal”);
    });

    }
    );

    After:
    require([
    ‘jquery’,
    ‘Magento_Ui/js/modal/modal’
    ],
    function($,modal) {
    var options = {
    type: ‘popup’,
    responsive: true,
    innerScroll: true,
    title: ”,
    buttons: [{
    text: $.mage.__(‘Close’),
    class: ”,
    click: function () {
    this.closeModal();
    }
    }]
    };

    // Check length of your div whether you declared div is available or not.
    if (jQuery(“#header-mpdal”).length != 0) {
    var popup = modal(options, $(‘#header-mpdal’));
    $(“#click-header”).on(‘click’,function(){
    $(“#header-mpdal”).modal(“openModal”);
    });

    } // end if (jQuery(“#header-mpdal”).length != 0) {
    // thanks https://jutesenthil.wordpress.com/2017/10/17/uncaught-typeerror-cannot-read-property-nodename-of-undefined-magento2/#comments
    }
    );

Leave a Reply