I have written a jQuery to hide a div in the content when page re-size (for mobile). It works fine in first time but when I reload the page, it shows hidden element(which I hide in jQuery).
This is my jQuery code:
$(window).resize(function () {
if ($(this).width() < 1200) {
$('.divHide').hide();
} else {
$('.divHide').show();
}
});
You have to write the function inside document ready.
ie,
$( document ).ready(function() {
if ($(this).width() < 1200) {
$('.divHide').hide();
}
else {
$('.divHide').show();
}
});
better way to do this is
function myfunction() {
if ($(this).width() < 1200) {
$('.divHide').hide();
}
else {
$('.divHide').show();
}
}
$(document).ready(myfunction);
$(window).on('resize',myfunction);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With