-1

I'm new to Javascript and need some help. Basically, I want the user to be able to enter their name into the input type box and when they press submit it should alert hello, then whatever the user's name is. The code already outputs the user's name as an alert but I don't know how to add the "hello" before the value. Please help, thanks.

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
<input type="text" id="t"></input>
<input type="submit" id="b"></input>


</body>
<script>
var t = document.getElementById("t");
document.getElementById("b").addEventListener("click", function() {
    alert(t.value);
});
</script>
</html>
Love2Code
  • 440
  • 6
  • 19
  • If I am understanding correctly. User submits their name then a alert box says hello once they press okay then their name is displayed in another alert box. Correct? – MTBthePRO May 23 '19 at 18:35

3 Answers3

2

You could use alert('hello ' + t.value);

1

JS uses the + operator for string concatenation. So you can build the complete string with "hello " + t.value and pass that to alert.

alert("hello " + t.value);
siralexsir88
  • 418
  • 1
  • 4
  • 14
1
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
  <title>Page Title</title>
</head>
<body>
<input type="text" id="t"></input>
<input type="submit" id="b"></input>


<script>
var t = document.getElementById("t");
document.getElementById("b").addEventListener("click", function() {
    alert("Hey " + t.value);
});
</script>
</body>
</html>

note , i added meta charset to the code, i don't know if its one of the problem but its safe to always add it, and your closing body i put at the end of the script because the body tags covers every code in your html.. i then concatenated using the "" +....notice i put a space after the hey in apostrophes to print out your name as: Hey Name instead of it being clustered like HeyName

upsidedownwf
  • 134
  • 1
  • 3
  • 12