1

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?

4
  • 3
    Get rid of = '' at the end of the line that assigns to msg. Commented May 19, 2021 at 21:52
  • 3
    You're setting msg to the empty string, not the element. Commented May 19, 2021 at 21:53
  • Donde that, and I get noting when fields match :-( Commented May 19, 2021 at 21:55
  • 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. Commented May 19, 2021 at 21:56

2 Answers 2

2

When you set msg, it should just be the result of calling getElementById(), not the innerHTML. And you shouldn't assign '' to it at the same time.

You were setting msg to an empty string, so you can't then use msg.innerHTML.

function hello() {
  var one = document.getElementById('one').value;
  var two = document.getElementById('two').value;
  var msg = document.getElementById('message');

  if (one !== two) {
    msg.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>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Barmar.... Excellent explanation. Thank You * 3
1

You can do it like this:

var one = document.getElementById('one');
var two = document.getElementById('two');
var msg = document.getElementById('message');

function hello() {
  msg.innerHTML = one.value === two.value ? "Fields match" : "Fields do not 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>

1 Comment

I Love shorthand comparisons, Thank You, Nenad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.