I'm adding a message to the right side of the input box using span/span and when I run these codes it's working well but when I enter the passwords the Confirm password line is moving to the left side. Is there is any way to fix that?
Thank you for your interest.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="jquery.min.js"></script>
<title>Document</title>
<style>
body{background-color: black; color: darkgreen;}
form{text-align: center;}
::placeholder{
color: red;
opacity: 0.5;
}
</style>
</head>
<body>
<form>
<p id="firstname">First name</p>
<input type="text" id="firstnameInput">
<p id="lastname">Last name</p>
<input type="text" id="lastnameInput">
<p id="email">E-mail</p>
<input type="text" id="emailInput">
<p>Password</p>
<input type="password" id="password" onkeyup="check()" required>
<p>Confirm password</p>
<input type="password" id="confirm_password" onkeyup="check()" required>
<span id='message'></span>
<br><br>
<input type="button" id="send" value="Send">
</form>
<script>
var check = function() {
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
document.getElementById('message').style.color = 'green';
document.getElementById('message').innerHTML = 'Matching';
} else {
document.getElementById('message').style.color = 'red';
document.getElementById('message').innerHTML = 'Not matching';
}
};
</script>
</body>
</html>
In general, it is better to adjust DOM element classes, and supply styling to match those classes, rather than manipulating styles directly.
Also, once you do a querySelector, if you are doing more than one thing with the element, you should save the result, rather than re-query.
TBH, I'm not sure which of these things made it work, but I'm not seeing the extreme left behavior any more.
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="jquery.min.js"></script>
<title>Document</title>
<style>
body{background-color: black; color: darkgreen;}
form{text-align: center;}
.valid {
color: green;
}
.invalid {
color: red;
}
::placeholder{
color: red;
opacity: 0.5;
}
</style>
</head>
<body>
<form>
<p id="firstname">First name</p>
<input type="text" id="firstnameInput">
<p id="lastname">Last name</p>
<input type="text" id="lastnameInput">
<p id="email">E-mail</p>
<input type="text" id="emailInput">
<p>Password</p>
<input type="password" id="password" onkeyup="check()" required>
<p>Confirm password</p>
<input type="password" id="confirm_password" onkeyup="check()" required>
<span id='message'></span>
<br><br>
<input type="button" id="send" value="Send">
</form>
<script>
var check = function() {
let message = document.getElementById('message');
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
message.classList.add('valid');
message.classList.remove('invalid');
message.innerHTML = 'Matching';
} else {
message.classList.add('invalid');
message.classList.remove('valid');
message.innerHTML = 'Not matching';
}
};
</script>
</body>
</html>
Your problem lies here <span id='message'></span>.
<span> is an inline element. means that it will always try to fit in the same line.
Use a block level element like <div> or <p> instead of span and your problem will be solved!