On Resize, I either call desktop Or mobile. I have a click function in each and they both seem to be running, if it was in desktop and then changes to mobile. I need to turn off desktop function if the mobile function is true and vice versa.
Here is a basic version of my code.
Screen_size()
$(window).resize(function() {
Screen_size()
});
function Screen_size(){
var w = $(window).width();
if (w <= 710){
Mobile();
}
if (w > 710){
Desktop();
}
}
function Mobile(){
console.log('Mobile');
$( "#open" ).click(function(e) {
console.log('Open Mobile');
});
}
function Desktop(){
console.log('Desktop');
$( "#open" ).click(function(e) {
console.log('Open Desktop');
});
}
Barbara Laird
12.7k2 gold badges48 silver badges58 bronze badges
asked Oct 18, 2016 at 0:11
Nathaniel Meyer
1801 gold badge1 silver badge10 bronze badges
1 Answer 1
No need for window resize .. just make a click event like this
$( "#open" ).on('click', Screen_size);
function Screen_size(){
var w = $(window).width();
(w <= 710) ? Mobile() : Desktop() ;
}
function Mobile(){
console.log('Open Mobile');
}
function Desktop(){
console.log('Open Desktop');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="open">Click</button>
answered Oct 18, 2016 at 0:38
Mohamed-Yousef
24k3 gold badges21 silver badges28 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
default
clickevent handler, and it adds up.$("#open").off("click").click(...)to remove the previous click handler and then bind a new one, but in my opinion it would be better to just bind a single click handler once and move theiftest inside the click handler (and not have a resize handler).