Search code examples
jqueryelementparent

Add Parent Class for default variables in jQuery


I am using jquery Mega Menu WordPress Plugin for my theme and I want to override the default values by pulling the parent element of the existing class see below code it is in PHP Page as you can see some of the variables in php.

echo 'jQuery(document).ready(function ($) {
    jQuery("#my_megamenu .menu").dcMegaMenu({
        classParent: ".mega",
        rowItems: "'.$mg_rowItems.'",
        speed   : "'.$mg_speed.'",
        effect  : "'.$mg_effect.'",
        event   : "'.$mg_event.'",
        '.$img_fullWidth.'
    });
});
';

Code Which I tried is below but didn't work.

echo 'jQuery(document).ready(function ($) {
    var parentContainer =  $('.dv-mega').parent('div');
    jQuery("#my_megamenu .menu").dcMegaMenu({
        classParent: parentContainer,
        rowItems: "'.$mg_rowItems.'",
        speed   : "'.$mg_speed.'",
        effect  : "'.$mg_effect.'",
        event   : "'.$mg_event.'",
        '.$mg_fullWidth.'
    });
});
';

Solution

  • You should use double quotes or escape the single quotes on your jQuery selector:

    $(".dv-mega").parent("div")

    OR

    $(\'.dv-mega\').parent(\'div\')

    Otherwise, the parser will interpret it as the end of the string.