0

I would like to use one if statement. How do I make one if statement?

if ($(window).width() <= 992) {
    //mycode
}

$(window).resize(function () {
    if ($(window).width() <= 992) {
        //mycode
    }
});
0

2 Answers 2

8

jQuery allows you to assign the event listener to multiple event types.

$(window).on('load resize', function(){
    if ($(window).width() <= 992) {
        //mycode
    }
})
Sign up to request clarification or add additional context in comments.

5 Comments

I was about to press the submit button with this approach! You beat me to it :D
Gotta be even quicker to find dupes heh?
Thank you for answering and contributing to Stack Overflow, however please take a second look at the question, for it seems to be a duplicate. Answering duplicate questions is not such a good idea as it disperses knowledge.
0

You could use your existing code, then trigger the resize() event afterwards:

$(window).resize(function () {
    if ($(window).width() <= 992) {
        //mycode
    }
}).resize();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.