0

JavaScript code is not reading my input in which is a name and should have output saying Maurice is a very nice name. I think their is a grammatical error that I am missing in my divOutput.

The program should take name input and output "Maurice is a very nice name"

//text box
function sayHi() {
  var txtName = document.getElementById("txtName");
  var divOutput = document.getElementById("divOutput");
  var name = txtName.value;
  divOutput.innerHTML = "<em>" + name + "</em>";
  divOutput.innerHTML = "is a very nice name.";
}
//end HI
<!DOCTYPE html>
<html lang="en-US">

<head>
  <meta charset="UTF-8">
  <title>Inner.html</title>
  <link rel="stylesheet" type="text/css" href="textBoxes.css" />
</head>

<body>
  <h1>Inner HTML </h1>
  <form action="">
    <fieldset>
      <label>Pleae type your name</label>
      <input type="text" id="txtName" />
      <button type="button" onclick="sayHi()">
         Click Me
        </button>
    </fieldset>
  </form>
  <div id="divOutput">
    Watch this space.
  </div>
</body>

</html>

2
  • divOutput.innerHTML will replace everything whatever present. Commented Jul 18, 2019 at 12:13
  • 1
    Either compose the full string before setting it as innerHTML or add it the second time, i.e. replace = with +=. Commented Jul 18, 2019 at 12:15

4 Answers 4

1

divOutput.innerHTML will replace whatever the divOutput have earlier, instead use +=.

function sayHi() {
  var txtName = document.getElementById("txtName");
  var divOutput = document.getElementById("divOutput");
  var name = txtName.value;
  divOutput.innerHTML += "<em>" + name + "</em>";
  divOutput.innerHTML += " is a very nice name.";
}
<form action="">
  <fieldset>
    <label>Pleae type your name</label>
    <input type="text" id="txtName" />
    <button type="button" onclick="sayHi()">
                     Click Me
                    </button>
  </fieldset>
</form>
<div id="divOutput">
  Watch this space.
</div>
</body>

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

Comments

0
function sayHi()
 {
 var txtName = document.getElementById("txtName") ;
 var divOutput = document.getElementById("divOutput") ;
 var name = txtName.value;
 divOutput.innerHTML = "<em>" + name + "</em> ";
 divOutput.innerHTML += "is a very nice name."; 
}

or

function sayHi()
 {
 var txtName = document.getElementById("txtName") ;
 var divOutput = document.getElementById("divOutput") ;
 var name = txtName.value;
 divOutput.innerHTML = "<em>" + name + "</em> is a very nice name."; 
}

Comments

0

Try this, you are overwriting html. Try this

function sayHi() {
        var txtName = document.getElementById("txtName");
        var divOutput = document.getElementById("divOutput");
        var name = txtName.value;
        divOutput.innerHTML = "<em>" + name + "</em> is a very nice name.";
        // divOutput.innerHTML = "is a very nice name.";
    }

Comments

0

Take a look at this code block where I modified your attempt just a tad.

In brief, you were almost there!

Note that in order to get the value of the element, you can use .value on what was retrieved from getElementById.

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="UTF-8" />
    <title>Inner.html</title>
    <link rel="stylesheet" type="text/css" href="textBoxes.css" />
  </head>
  <body>
    <h1>Inner HTML</h1>
    <form>
      <fieldset>
        <label>Please type your name</label>
        <input type="text" id="txtName" />
        <button type="button" onclick="sayHi()">
          Click Me
        </button>
      </fieldset>
    </form>
    <div id="divOutput">
      Watch this space.
    </div>
    <script type="text/javascript">
      //text box
      function sayHi() {
        var txtName = document.getElementById("txtName");
        console.log(txtName.value); // <== use dot value to get the value.
        var divOutput = document.getElementById("divOutput");
        divOutput.innerHTML =
          "<em>" + txtName.value + "</em> is a very nice name.";
      }
      //end HI
    </script>
  </body>
</html>

Comments

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.