0

I have a form that collects first name and last name via two div elements:

<div class="form-row">
  <div class="col">
    <input type="text" class="form-control" id="lastName" placeholder="last name" />
  </div>
</div>

<div class="form-row">
  <div class="col">
    <input type="text" class="form-control" id="firstname" placeholder="first name" />
  </div>
</div>

In the script I have the following line

const lastName = document.getElementById('lastName').value;

What I would like to do is produce a constant called fullname which comprises of firstname+lastname

How can I rewrite the line starting with const above to do this?

0

2 Answers 2

4

This one should be fairly simply. You already have a way of extracting the value of the lastName input. So just do the same for firstName and then concatenate the two variables.

const firstName = document.getElementById('firstName').value;
const lastName = document.getElementById('lastName').value;

const fullName = `${firstName} ${lastName}`;
Sign up to request clarification or add additional context in comments.

1 Comment

I noticed that in the HTML you have first name spelled with a lowercase "n" instead of an uppercase"N". If you had tried this solution and it didn't work that may have been your problem.
1

Using your existing syntax:

const fullName = document.getElementById('firstname').value + ' ' + document.getElementById('lastName').value;

There are of course other ways to get these values and this assumes there will be a value but you get the gist.

Comments

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.