Merge text inputs to preview content prior to form submission

I've been on the hunt for a solution to display the collective values entered into multiple text box fields as they are being typed. Currently, I have 6 text boxes (description1, description2, description3, description4, description5, description6) which are concatenated using PHP into a "final_description" that is then stored in the database. My objective is to provide users with a preview of what the final_description will look like when all 6 fields are combined, before submitting the form and processing it through PHP. The kind of code I'm seeking is akin to the functionality seen here on StackOverflow where typing adds content below for visual output. I simply want to merge the input from multiple text boxes into one preview.

Answer №1

If you have the following elements:

<input class="desc" id="desc1" />
<input class="desc" id="desc1" />
<input class="desc" id="desc1" />
<input class="desc" id="desc1" />

<div id="final_desc" />

You can make it work like this:

window.onload = function(){
   function forEach(arr, fun){ return Array.prototype.forEach.call(arr, fun); };
   var inputs = document.querySelectorAll(".desc");
   function updateFinalDesc(){
       var finalDescription = "";
       forEach(inputs, function(inp){ finalDescription += inp.value + "<br/>"; });
       document.getElementById('final_desc').innerHTML = finalDescription;
   };
   forEach(inputs, function(input){
       input.addEventListener('keypress', updateFinalDesc);
       input.addEventListener('change', updateFinalDesc);
   });
}

I hope this explanation is helpful. Cheers!

Answer №2

For those utilizing the knockout.js data binding framework, here is a solution for your specific scenario:

<div id="content">
    <input type="text" data-bind="value: detail1" />
    <input type="text" data-bind="value: detail2" />
    <input type="text" data-bind="value: detail3" />
    <input type="text" data-bind="value: detail4" />
    <input type="text" data-bind="value: detail5" />
    <input type="text" data-bind="value: detail6" />
    <span data-bind="text: combined"></span>
</div>

<script type="text/javascript">
function myDataModel()
{
    var self = this;
    self.detail1 = ko.observable("");
    self.detail2 = ko.observable("");
    self.detail3 = ko.observable("");
    self.detail4 = ko.observable("");
    self.detail5 = ko.observable("");
    self.detail6 = ko.observable("");

    self.combined= ko.computed(function() {
    return self.detail1() + " " + self.detail2()+ " " + self.detail3()+ " " + self.detail4()+ " " + self.detail5()+ " " + self.detail6();
    }, self);
}

ko.applyBindings(new myDataModel(), document.getElementById("content"));
</script>

To ensure proper functionality, remember to integrate the knockout.js library into your webpage.

Answer №3

My approach might not be perfect, but it gets the job done:

var desc1 = document.getElementById('description1');
var desc2 = document.getElementById('description2');
var desc3 = document.getElementById('description3');
var desc4 = document.getElementById('description4');
var desc5 = document.getElementById('description5');
var desc6 = document.getElementById('description6');
desc1.onkeyup = desc1.onkeypress = function(){
document.getElementById('description1preview').innerHTML = this.value;
}
desc2.onkeyup = desc2.onkeypress = function(){
document.getElementById('description2preview').innerHTML = this.value;
}
desc3.onkeyup = desc3.onkeypress = function(){
document.getElementById('description3preview').innerHTML = this.value;
}
desc4.onkeyup = desc4.onkeypress = function(){
document.getElementById('description4preview').innerHTML = this.value;
}
desc5.onkeyup = desc5.onkeypress = function(){
document.getElementById('description5preview').innerHTML = this.value;
}
desc6.onkeyup = desc6.onkeypress = function(){
document.getElementById('description6preview').innerHTML = this.value;
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Analyzing the path of the cursor

Looking to enhance my tracking capabilities by monitoring mouse movements. I have the ability to capture XY coordinates, but understand that they may vary depending on browser size. Are there any other parameters recommended for accurate results? P.S: Be ...

There is no way to convert a strongly typed object into an observable object using MobX

I have been utilizing the MobX library in conjunction with ReactJS, and it has integrated quite smoothly. Currently, I am working with an observable array structured as follows: @observable items = []; When I add an object in the following manner, everyt ...

AngularJS $cookies version 1.6.9 experiencing functionality issues

I recently implemented AngularJS $cookies 1.6.9 in my website to handle cookie management. To test it out, I attempted a basic cookie set like this: $cookies.put('myCookieTest', 'test'); var cookie = $cookies.get('myCookieTest&apo ...

Utilizing the variables defined in the create function within the update function of Phaser 3

I'm facing an issue in my game where I can't access a variable that I declared in the create function when trying to use it in the update function. Here is a snippet of what I'm trying to achieve: create() { const map = this.make. ...

The Laravel array is returning incorrect values

After making an Ajax request, I receive a response in the form of an array of images. The issue I am facing is that when I echo out the values of the array during a foreach loop, they appear to be correct. However, once the loop ends and I examine the cont ...

Exploring the concept of front-end deletion through ajax technology

I'm currently tackling a front-end CRUD RESTful API project and encountering challenges specifically related to the DELETE and PUT methods. While I have successfully managed to implement the GET and POST functionalities, utilizing a forms.html file f ...

Using JavaScript to generate dynamic folders in Alfresco is not functioning as expected

Working with Alfresco 4.0.d community edition (also tested on Alfresco 4.0.c) on an Oracle Linux 64-bit virtual machine using Firefox. I've been developing a script to dynamically create sub-folders as new items are added to a space/folder via a rule ...

JSON - The challenge of incorporating single quotes within double quotes

In my current scenario, I am using the following code to populate form fields. The code is designed to handle a JSON dataset that has been encoded with PHP's json_encode function. Everything works smoothly when dealing with either single or double qu ...

Steps to delete an item from a table without affecting the database

I've successfully set up a mysql database and integrated it with my HTML table using AJAX to both save data in the db and retrieve data from it. Additionally, I have implemented a feature where each record in the HTML table has a remove button that us ...

Generated Form Data Automatically

Seeking advice on achieving a specific functionality in ASP.NET Web Form that is similar to the AutocompleteExtender, but requires more flexibility. Here is what I aim to accomplish: There are 2 TextBox fields on the form: CompanyName and CompanyRef (a u ...

Steps for integrating a valid SSL certificate into a Reactjs application

After completing my ReactJS app for my website, I am now ready to launch it in production mode. The only hurdle I face is getting it to work under https mode. This app was developed using create-react-app in a local environment and has since been deployed ...

Which is the better option for data storage: JSON files or MySQL database?

I am currently developing a project that utilizes the vis.js JavaScript framework to showcase a visual network of various categories. There are approximately 2000 categories available for selection, each with a substantial amount of associated data. I am ...

The children's className attribute can impact the parent element

As I work on creating a card object, I envision it with the className .card that is styled in CSS as follows: .card img{position:absolute; width:150px; height:160px} I want only the images inside my div to overlap each other while not affecting the divs ...

How can one keep object values constant or unchanging in JavaScript?

Here is a simple example where I need to clarify my requirement. I want to ensure that the values of object a are not changed after assigning it to constant b and modifying its values. I want the console to display the original values of object a, not the ...

Animation not functioning properly after Ajax call in event onload

After making an AJAX call to load all the data, I encountered an error in the response data. The code snippet likeObj('#like'+postid).effect("bounce", {times:1,distance:25},400); throws an error stating that likeObj is not a function. I'm s ...

How do I automatically redirect to a different URL after verifying that the user has entered certain words using Javascript?

I want to create a function where if a user input on the "comments" id matches any word in my FilterWord's array, they will be redirected to one URL. If the input does not match, they will be redirected to another URL. The checking process should onl ...

The chosen option does not display any information

I am new to databinding an html control using ajax for the first time. After checking and debugging my ajax call, I can see that the data is retrieved successfully, but it does not show up in the select option. My javascript code is positioned at the botto ...

Enhance the axis functionality in c3.js

My goal is to display a user's financial data for the previous week, but sometimes they may not have data for all seven days. When using c3.js, I noticed that it automatically extends the 'endpoint-value' to the end of the axis. Is there a w ...

Setting the class attribute dynamically using code behind

Below is a snippet of code I am working with: div1.Attributes.Add("class", "displayNone"); It functions properly during page load but fails to do so during an OnClick event. The issue arises from the fact that my HTML element <div id="div1"></d ...

Exploring the process of updating initialState within react-redux

I am currently using react-redux to retrieve data from a MongoDB database and integrate it into my React App. Below is the structure I am working with: const initialState = { Level: [{ wId: Math.random(), Level1: [ { id: Math.rando ...