I have an alert that displays on a website, on specific days of the year.
My question is this: How can I hide that alert IF the date is not one of those days? I was thinking the below code would work, but it only delays the alert based on a condition in my code I can see, but I'm not sure how to work around that.
$(document).ready(function() {
/* if (typeof window.sessionStorage != undefined) {
if (!sessionStorage.getItem('mySessionVal')) { */
$('<div />', {
id : "alertmsg",
text :"",
on : {
click: function() {
$(this).slideUp(500);
}
}
}).appendTo('body').slideDown(1).delay(6000).slideToggle("fast");
sessionStorage.setItem('mySessionVal', false);
sessionStorage.setItem('storedWhen', (new Date()).getTime());
var now = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var month = now.getMonth();
var date = now.getDate();
var year = now.getFullYear();
var msg;
if (month == 0, date ==1) msg = "Submissions for our January issue have now closed.";
else if (month == 3, date ==1) msg = "Submissions for our April issue have now closed.";
else if (month == 6, date ==1) msg = "Submissions for our July issue have now closed.";
else if (month == 9, date ==1) msg = "Submissions for our October issue have now closed.";
else $('#alertmsg').hide();
$('#alertmsg').text(msg);
});
Not sure where you got if (month == 0, date ==1)
from but it should be if (month == 0 && date == 1)
. This should be the same for each of the if
s
Your last line after the if statement is
$('#alertmsg').text(msg);
Meaning that after you check for the month name regardless to the content of msg
you are putting the text inside the div.
You can do something like this:
var now = new Date();
var days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
var month = now.getMonth();
var date = now.getDate();
var year = now.getFullYear();
var msg;
if (month == 0, date ==1) msg = "Submissions for our January issue have now closed.";
else if (month == 3, date ==1) msg = "Submissions for our April issue have now closed.";
else if (month == 6, date ==1) msg = "Submissions for our July issue have now closed.";
else if (month == 9, date ==1) msg = "Submissions for our October issue have now closed.";
if(msg != '') {
// append the div to body here and assign the msg into it
}
I'm trying to learn JQuery, so I didn't have a good idea of how to solve the above issue, but I think I finally did! I just added the below code after my final IF statement.
else $('#alertmsg').removeAttr('id');
$('#alertmsg').text(msg);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.