Reputation: 374
I have a #div1
, which when clicked should toggle #div2
with jquery. This click event should only execute the function if the window size is below 760px. I'm bad at javascript so I'm getting wrong results... Instead of toggling the div when the user clicks on the #div1
, it toggles itself on, when you resize the window below 760px.
I'm sure there's something obvious wrong with the order of the if-statement or something.
That's my code:
$(function(){
if($(window).width() <= 760){
$('#div').click(function(){
$('#div2').toggle({"display":"block"});
});
}
});
Hope my question is okay to understand.
Thank you!
Upvotes: 0
Views: 2580
Reputation: 155
What if you put the if inside the .click() ?
$(function(){
$('#div').click(function(){
if($(window).width() <= 760)
{
$('#div2').toggle({"display":"block"});
}
});
});
Hope it helps!
Upvotes: 1
Reputation: 9845
Your order is a little mixed up. It should be
$(function(){
$('#div').click(function(){
if($(window).width() <= 760){
$('#div2').toggle({"display":"block"});
}
});
});
Upvotes: 2