Changing the CSS property position via javascript


There is an adaptive fixed menu

    <script type="text/javascript">
$(document).ready(function(){ 
var touch   = $('#touch-menu');
var menu    = $('#menu ul');

$(touch).on('click', function(e) {
    e.preventDefault();
    menu.slideToggle();
});

$(window).resize(function(){
    var w = $(window).width();
    if(w > 790 && menu.is(':hidden')) {
        menu.removeAttr('style');
    }
});
});
</script>

You need to change the position property from fixed to absolute for the #menu block when you click on #touch-menu.

I tried it like this

<script type="text/javascript">
$(document).ready(function(){ 
    var touch   = $('#touch-menu');
    var menu    = $('#menu ul');
    var msld    = $('#menu');

    $(touch).on('click', function(e) {
        e.preventDefault();
        menu.slideToggle();
        msld.style.position = "absolute";
    });

    $(window).resize(function(){
        var w = $(window).width();
        if(w > 790 && menu.is(':hidden')) {
            menu.removeAttr('style');
        }
    });

});
</script>

Returns the error

Uncaught TypeError: Cannot set property 'position' of undefined

Author: Nicolas Chabanovsky, 2015-10-27

2 answers

msld.style.position = "absolute";

Choose one of the following options. Probably the first one.

msld.css('position', "absolute");
msld[0].style.position = "absolute";
 0
Author: Qwertiy, 2015-10-28 07:46:39
msld.css('position', 'absolute');
 2
Author: walik, 2015-10-27 16:41:47