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
  • 18,263
  • 7
  • 55
  • 75
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • 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,413
  • 2
  • 21
  • 41
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
  • 131,976
  • 10
  • 91
  • 107
0

Do this :

 name.onkeyup = function(){
        div.value = 'Hello ' + this.value;
    }
Siddharth
  • 505
  • 2
  • 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
  • 3,617
  • 1
  • 23
  • 19