I want to reuse a var to load a new string.
So here are my attempts.
function hello() {
var one = document.getElementById('one').value;
var two = document.getElementById('two').value;
var msg = document.getElementById('message').innerHTML = '';
if (one !== two) {
document.getElementById('message').innerHTML = "Fields do not match";
return false;
} else {
msg.innerHTML = "Fields match";
return false;
}
}
<form action="#" onsubmit="return hello()">
<input type="text" name="one" id="one">
<input type="text" name="two" id="two">
<input type="submit" value="Test this thing">
</form>
<p id="message"></p>
I've already declared the message var outside the function, so I don't want to repeat the full row inside the function...
Any suggestions?
= ''at the end of the line that assigns tomsg.msgto the empty string, not the element.var msg = document.getElementById('message'); msg.innerHTML = 'Fields match'; if (one !== two) { msg.innerHTML = 'Fields do not match'; return false; } return true;. That's all you need.