JS if condition is executed only if the condition is true on loading of the file

Viewed 47

Though there were similar questions answered on this topic,The problem didn't seem to get solved for me,

My function (go) works only if the size of the window is satisfying the if condn. during the loading of the file

I even tried using resize() event in jquery <which didn't work>

FYI I'm a beginner,Here is my code

JS

let topbar_children  = $('.topbar').children(); 

var media_width_600px = window.matchMedia("(max-width:600px)").matches;

var media_width_grt_600px = window.matchMedia("(min-width:600px)").matches;




 function go () {
    if (media_width_600px ) {
        var div_left = topbar_children[0];
        $(topbar_children[0]).detach();
        var dropdown_div = document.createElement('div');
        dropdown_div.classList.add('left-side');
        dropdown_div.appendChild(div_left);
        dropdown_div.classList.add('dropdown_content_500px');
        console.log(topbar_children);
        document.body.prepend(dropdown_div);

    } if(media_width_grt_600px ){
       
        dropdown_div.removeChild(div_left);
    }
       
    

 }
 
go();

Also mention a way to refactor my code if possible

Check this github repo https://github.com/antonrejoe/Oliver_orchard getting_responsive branch

Thanks in advance

0 Answers
Related