0

I want a user to be able to enter specific values into an input field and would like to be able to output the values WITH HTML "markup" such as '< div >' and '< p >' as text only. I have tried outputting it as a string as well but i cannot figure it out. Please help!

I would like it to operate like this:

Input value = x Output = x

does it have to be output as a string so the browser doesnt only output = x? All insight helps!

Please See My Example Here:

<html>
<head>
  <title>Markup Generator</title>
</head>
<body>
 
First name: <input id="first_name">
Last name: <input id="last_name">
<button id="say">Say hi!</button>
 
<hr>
<div id="result"></div>
 
<script>
function say_hi() {
    var fname = document.getElementById('first_name').value;
    var lname = document.getElementById('last_name').value;
 
    var html = 'Hello <b>' + fname + '</b> ' + lname;
 
    document.getElementById('result').innerHTML = html;
}
 
document.getElementById('say').addEventListener('click', say_hi);
</script>

1
  • just replace innerHTML to innerText. Commented Jan 6, 2019 at 5:30

2 Answers 2

2

<html>
<head>
  <title>Markup Generator</title>
</head>
<body>
 
First name: <input id="first_name">
Last name: <input id="last_name">
<button id="say">Say hi!</button>
 
<hr>
<div id="result"></div>
 
<script>
function say_hi() {
    var fname = document.getElementById('first_name').value;
    var lname = document.getElementById('last_name').value;
 
    var html = 'Hello <b>' + fname + '</b> ' + lname;
 
    document.getElementById('result').innerText = html;
}
 
document.getElementById('say').addEventListener('click', say_hi);
</script>

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

1 Comment

perfect! thats exactly what i needed. youre the best!
0

You can use innerText instead of innerHTML like this

document.getElementById('result').innerText = 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.