0

I have a hidden element called ShowUsername that I want to show if the user inputs a username that is less than 8 characters. I can't seem to figure this out if someone could help me out. I currently have the element hidden and it stays hidden if the string input is 8 or more characters.

var myPassword = document.getElementById("Password");
var myUserName = document.getElementById("Username");

document.getElementById("frmRegister").onsubmit = function() {

  <!-- Username must be 8 or more characters -->
  if (myUserName.value.length >= 8) {
    alert("YOur Username was saved");
  } else {
    ShowUsername.display;
  }
}
Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
  • Can you share the html too? – obscure Apr 07 '21 at 21:15
  • 1
    I've taken the liberty of adding a Stack Snippet with the JavaScript code you had provided. You may want to [edit] your question, and the snippet (there should be a "edit the above snippet" link below it) to add the HTML elements so that people can reproduce the issue. – Heretic Monkey Apr 07 '21 at 21:15
  • 1
    Note that `ShowUsername.display;` does nothing. If you have an HTML element with the ID "ShowUsername` you could get away with `ShowUsername.style.display = "block";`... – Heretic Monkey Apr 07 '21 at 21:17
  • What do you mean by hidden, `display:none;`, or ``? – StackSlave Apr 07 '21 at 21:26

2 Answers2

0

Please try like this.

ShowUsername.style.display = 'block';
0

set the default display in html using style="display:none;" and use style.display = 'block' or style = 'display:block;' to show

example

window.btn.onclick = function() {
  if (window.Username.value.length >= 8) {
    window.ShowUsername.style = 'display:none;';
    console.log("YOur Username was saved");
  } else {
    window.ShowUsername.style = 'display:block;';
  }
}
<input id="Username">
<div id="ShowUsername" style="display:none;"> min 8 chars</div>
<div><button id="btn" type="button">BTN</button></div>
Daniel
  • 34,125
  • 17
  • 102
  • 150