Formatting Field Combining Two Fields: Formatting Outcome
In this article, we will discuss how to combine two fields in a form and format the outcome. The focus of this article is on a global topic that can be applied to any website. By the end of this article, you will have a solid understanding of how to combine fields and format the results, including code examples and best practices.
Combining Two Fields
When creating a form, you may need to combine the values of two fields to create a new piece of information. For example, you might have a form that asks for a person's first and last name. To create a full name, you would need to combine the first and last name fields.
In HTML, you can use the concat() function to combine the values of two fields. Here is an example:
<form> \
<label for="firstName">First Name:</label> \
<input type="text" id="firstName"> \
<br> \
<label for="lastName">Last Name:</label> \
<input type="text" id="lastName"> \
<br> \
<label for="fullName">Full Name:</label> \
<input type="text" id="fullName" value="<concat(document.getElementById("firstName").value, " \
", document.getElementById("lastName").value)>"> \
<br> \
<input type="submit" value="Submit"> \
</form>
In this example, we have a form that asks for a first name, last name, and full name. The full name field uses the concat() function to combine the values of the first and last name fields.
Formatting the Outcome
Once you have combined the values of two fields, you may want to format the outcome. For example, you might want to capitalize the first letter of each name in the full name field.
In HTML, you can use the toUpperCase() and substring() functions to format the outcome. Here is an example:
<form> \
<label for="firstName">First Name:</label> \
<input type="text" id="firstName"> \
<br> \
<label for="lastName">Last Name:</label> \
<input type="text" id="lastName"> \
<br> \
<label for="fullName">Full Name:</label> \
<input type="text" id="fullName" value="<concat(document.getElementById("firstName").value.substring(0,1).toUpperCase(), " \
", document.getElementById("firstName").value.substring(1), " ", document.getElementById("lastName").value.substring(0,1).toUpperCase(), " \
", document.getElementById("lastName").value.substring(1))>"> \
<br> \
<input type="submit" value="Submit"> \
</form>
In this example, we have updated the full name field to capitalize the first letter of each name. We have done this by using the substring() function to get the first letter of each name and the toUpperCase() function to capitalize it.
Key Concepts
concat() function in HTML.toUpperCase() and substring() in HTML.In this article, we have discussed how to combine two fields in a form and format the outcome. By using the concat() function in HTML, we were able to combine the values of two fields. We then used various functions such as toUpperCase() and substring() to format the outcome. By combining and formatting fields, we can create dynamic and user-friendly forms.
References
- HTML and CSS: Design and Build Websites. Jon Duckett.
- Combining input values with JavaScript. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/concat
- JavaScript String Methods - W3Schools. https://www.w3schools.com/js/js_string_methods.asp