I'm trying to get an alert box to appear with a message contain both pre-defined text and user inputed text.
What I have so far are two input fields for first and last name and a submit button. It currently shows the alert popup with the pre-defined text but the user input just comes up as 'undefined'. Could you let me know where I might have gone wrong?
The code is as follows:
<form>
<p>First name: <input type="text" id="fname" name="firstname" value=""></p>
<p>Last name: <input type="text" id="lname" name="lastname" value=""></p>
<p><input type="button" value="Submit" id="namebutton" /></p>
<script>
var fname = document.getElementById("fname").value;
var lname = document.getElementById("lname").value;
document.getElementById("namebutton").addEventListener("click", function() {
alert('Hello ' + fname.value);
})
</script>
</form>