0

I am doing a project for school and I need a form with at least two text entries with JavaScript code to request, accept and manipulate the text of the form. so this is what I have come up with and I have spent more time on this than I would like to admit.

I cannot get it to define the variable fullname. Help would be appreciated from this rookie. If I knew any less about coding I wouldn't be coding at all.

<head>
<script>
function sayHi(){
var firstName = document.getElementById("firstName");
var lastName = document.getElementById("lastName");
var fullName = firstName + lastName;
var name = fullName.value;
var txtOutput = document.getElementById("txtOutput");

txtOutput.value = "Hi there, " + name + "!"
}

</script>
<style>
legend {
   background-color: #000;
   color: #fff;
   padding: 3px 6px;}

   .txtoutput {
   font: 1rem 'Fira Sans', sans-serif;
}

input {
   margin: .4rem;
}


</style>
<body>
  <form action = "">
  <fieldset>
  <legend>Tell us your name</legend>
  <label>First: </label>
  <input type = "text" id = "firstName" />
  <label>Last:</label>
  <input type="text" id="lastName"/>
  <input type = "button" value = "click me" onclick = "sayHi()"/>
  <input type = "text"  id = "txtOutput" />
 </form>

</body>
</html>

1 Answer 1

1

The problem is that you are trying to concatenate two DOM elements (firstName and lastName). You should get their input values (firstName.value and lastName.value) and then concatenate.

EDIT: also, there is no need to have fullName.value when variable fullName already holds the string value. Result function would look like this:

function sayHi(){
    var firstName = document.getElementById("firstName");
    var lastName = document.getElementById("lastName");
    var fullName = firstName.value + " " + lastName.value;
    var txtOutput = document.getElementById("txtOutput");
    txtOutput.value = "Hi there, " + fullName + "!"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the quick response. That did it. + " " + Helped with display as well. Thanks again!

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.