The way this code is now means that I’d have to manually add to it code if the number of alerts increases. How can I combine the code below to adjust on its own to accommodate any number of alerts.
// Alert session cookie starts here
$(document).ready(function(){
$('.alert-box').attr('id', function(i) {
return 'alert'+(i+1);
});
function closeBox(){
var closeBox = $('#alert1').remove();
// - Notice how we pass a function and get an integer
$.cookie('Box-closed', true, {path: '/'});
}
// - Simply checks the alert box above
if($.cookie('Box-closed')){
console.log('Closing the alert box automatically...');
closeBox();
}
// Close the box on click
$('#alert1 .alert-switch').click(function () {
closeBox();
});
function closeBox2(){
var closeBox2 = $('#alert2').remove();
// - Notice how we pass a function and get an integer
$.cookie('Box2-closed', true, {path: '/'});
}
// - Simply checks the alert box above
if($.cookie('Box2-closed')){
console.log('Closing the alert box automatically...');
closeBox2();
}
// Close the box on click
$('#alert2 .alert-switch').click(function () {
closeBox2();
});
function closeBox3(){
var closeBox3 = $('#alert3').remove();
// - Notice how we pass a function and get an integer
$.cookie('Box3-closed', true, {path: '/'});
}
// - Simply checks the alert box above
if($.cookie('Box3-closed')){
console.log('Closing the alert box automatically...');
closeBox3();
}
// Close the box on click
$('#alert3 .alert-switch').click(function () {
closeBox3();
});
});