1

I am trying to write an HTML5 - JS Program that displays an Error Message IF the User Input is Empty. But I am not able to get the "Error Message", even thou I can't seem to find any issues.

I am a complete beginner and the Video Tutorial I am using doesn't have any User Support.

Please Help.

I have tried all I know but no avail.

<!DOCTYPE html>
<html>
<head>
    <title>Error Message Display</title>
    <script type="text/javascript">
        function displayInput()
        {
            var testInput = document.getElementById("name").value;
            if (testInput.lenght == 0)
        {
            document.getElementById("para").innerHTML = "NOOOO";
        }else
            document.getElementById("para").innerHTML = testInput;
       }
   </script>
</head>
<body>
    <input id="name" type="text"/>
    <button onclick="displayInput();">Show The Text</button>
    <p6 id="para"></p6>
</body>
</html>

If the Input is Empty an Error Message i.e NOOOO should display.

2
  • testInput.lenght is a typo Commented Aug 2, 2019 at 8:45
  • Thank You for teaching me. I will focus on that part from now on. Commented Aug 2, 2019 at 8:50

1 Answer 1

1

hi you made spelling mistake in length property of element

it should not be lenght

function displayInput()
{
  let el = document.getElementById("name");
  let para = document.getElementById("para")
  var testInput = el.value;
  if (testInput.length == 0)
  {
    para.innerHTML = "NOOOO";
  }
  else{   

    para.innerHTML = testInput;
  }
}
<!DOCTYPE html>
<html>
<head>
    <title>Error Message Display</title>
</head>
<body>

<input id="name" type="text"/>

<button onclick="displayInput();">Show The Text</button>

<p6 id="para"></p6>

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

3 Comments

Thank You for teaching me. I will keep that in mind in the future.
Hi @CodingNoob I have also updated your code, to reduce the memory utilization and loading the element into variables once and reduce code repitation
Big Help. Thank you. Although I need some more learning to utilize it fully, I am grateful.

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.