You can add data-bind attribute for the inputs, and then save their values to array and keep watching them. Finally, render them on change, or page load.
This code will save to array and watch the inputs that have data-bind, and will trigger updateDisplay event on any input change:
// Declare a global object to store view data.
var viewData = {};
(function () {
// Update the viewData object with the current field keys and values.
function updateViewData(key, value) {
viewData[key] = value;
}
// Register all bindable elements
function detectBindableElements() {
var bindableEls;
bindableEls = $('[data-bind]');
// Add event handlers to update viewData and trigger callback event.
bindableEls.on('change', function () {
var $this;
$this = $(this);
updateViewData($this.data('bind'), $this.val());
$(document).trigger('updateDisplay');
});
// Add a reference to each bindable element in viewData.
bindableEls.each(function () {
updateViewData($(this), $(this).val());
});
}
// Trigger this event to manually update the list of bindable elements, useful when dynamically loading form fields.
$(document).on('updateBindableElements', detectBindableElements);
detectBindableElements();
});
Now we want to update the review fields on updateDisplay event, so we add data-update to review fields with the same value added in data-bind for the input.
This code will go through all data-update and set their values:
function updateDisplay() {
var updateEls;
updateEls = $('[data-update]');
updateEls.each(function () {
$(this).text(viewData[$(this).data('update')]);
});
}
// Run updateDisplay on the callback.
$(document).on('updateDisplay', updateDisplay);
Example:
// In your form
<input name="email" data-bind="email">
...
...
// In review fields
<div data-update="email"></div>