I am working on creating a simple alert message with specific background colors depending on the user's login status. If a user fails to login, the alert message background-color should be red; if a user successfully logs in, the alert background-color should be green. I need help figuring out how to switch between using the 'alertsuccess' div and the 'alert' div based on the login outcome.
<div id="alert" style="display:none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">& times;</span>
<p id="alertmsg"> </p>
</div>
<div id="alertsuccess" style="display:none;">
<span class="closebtn" onclick="this.parentElement.style.display='none';">& times;</span>
<p id="alertmsg"> </p>
</div>
<input type="text" id="username" placeholder="Username" name="uname" maxlength="15" required>
<button id="button123" onclick="showalert('alert');">Submit</button>
Javascript Function:
function showalert(id){
var divelement = document.getElementById(id);
var username = document.getElementById("username").value;
if (username === ""){
document.getElementById("alertmsg").innerHTML = "You didn't insert a username!";
divelement.style.display =='none'
divelement.style.display = 'block';
}
else if(username === "u1460714"){
document.getElementById("alertmsg").innerHTML = "Successfully logged in";
divelement.style.display =='none'
divelement.style.display = 'block';
}
}
CSS Styling:
#alert {
height:100px;
width:300px;
background-color: #f44336;
color: white;
position: fixed;
}
.closebtn {
margin-left: 15px;
color: white;
font-weight: bold;
float: right;
font-size: 22px;
line-height: 20px;
cursor: pointer;
transition: 0.3s;
}
.closebtn:hover {
color: black;
}
#alertmsg{
font-family:sans-serif;
font-size:15px;
text-align: center;
}
#alertsuccess {
background-color: #4CAF50;
}