Reputation: 397
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?
Upvotes: 0
Views: 971
Reputation: 3133
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.
Upvotes: 1
Reputation: 1652
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}`;
Upvotes: 4