1

When a user types in an input box, I want their name to be displayed after the text 'hello'. Instead of doing this, the word 'hello' is removed and replaced by their name.

var name = document.getElementById('username');
var div = document.getElementById('displayname');

name.onkeyup = function() {
  div.value = name.value;
}
<input type="text" placeholder="Name" id="username">
<input type="submit" id="displayname" value="Hello">

JsFiddle: https://jsfiddle.net/t01prhx4/

Sebastian Simon
  • 16,564
  • 7
  • 51
  • 69
The Codesee
  • 3,628
  • 3
  • 32
  • 71
  • 1
    There’s one subtle error in your code in the snippet: [do not use the variable name `name`](http://stackoverflow.com/q/10523701/4642212) (in a global scope), as it is a global string property of `window`. – Sebastian Simon Jul 02 '16 at 11:27

4 Answers4

0

You can do it like this:

name.onkeyup = function(){
    div.value = 'Hello ' + name.value;
} 
D.Dimitrioglo
  • 3,023
  • 2
  • 20
  • 35
0

Simple add hello before the

var name = document.getElementById('username');
var div = document.getElementById('displayname');

name.onkeyup = function(){
   div.value = "Hello " + name.value;
} 

https://jsfiddle.net/98c16uv5/

ScaisEdge
  • 129,293
  • 10
  • 87
  • 97
0

Do this :

 name.onkeyup = function(){
        div.value = 'Hello ' + this.value;
    }
Siddharth
  • 465
  • 1
  • 4
  • 9
-1

Is this what you want?

var name = document.getElementById('username');
var div = document.getElementById('displayname');

name.onkeyup = function(){
    div.value = 'Hello ' + name.value;
} 

https://jsfiddle.net/oapfc1Lo/

AlexG
  • 2,920
  • 21
  • 17