Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
Reusable function to display input text in div

I have a function that displays text in a div as its typed into an input. Right now it simply checks for each ID go get the value and display the text.

I want to make this function reusable so that I can match different inputs with different divs without writing a unique function for each case.

Here is an example that works using a single input and div:

<body>

  <input type='text' name='name' id='inputBox'>
  <div id='displayBox'></div>

  <script type="text/javascript">
    var displayText = document.getElementById('inputBox');
    displayText.onkeyup = function() {
      document.getElementById('displayBox').innerHTML = inputBox.value;
    }
  </script>
</body>

And I want to be able to repeat this for different sets of unique inputs & divs with a reusable function.

<body>

  <!-- First set -->
  <input type='text' name='name' id='inputBox'>
  <div id='displayBox'></div>

  <!-- Second set -->
  <input type='text' name='name' id='inputBox'>
  <div id='displayBox'></div>

  <!-- etc... -->

  <script type="text/javascript">
    var displayText = document.getElementById('inputBox');
    displayText.onkeyup = function() {
      document.getElementById('displayBox').innerHTML = inputBox.value;
    }
  </script>
</body>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you wrap each "set" in a container, and swap your ids for classes, you can can add listeners to each input to watch for changes, find the parent container, find the display box and update its text content.

// Get all of the inputs
const displayText = document.querySelectorAll('.inputBox');

// Attach listeners to all of them
displayText.forEach(input => {
  input.addEventListener('keyup', handleChange, false);
});

function handleChange() {

  // Find the closest div ancestor element (the container)
  const parent = this.closest('div');

  // Then locate the display box and update the text content
  parent.querySelector('.displaybox').textContent = this.value;
}
.container { margin-bottom: 1em; }
.displaybox { margin-top: 0.2em; height: 1.3em; width: 300px; border: 1px solid black; }
<div class="container">
  <input type="text" name="name" class="inputBox" placeholder="Type here">
  <div class="displaybox"></div>
</div>
<div class="container">
  <input type="text" name="age" class="inputBox" placeholder="Type here">
  <div class="displaybox"></div>
</div>
<div class="container">
  <input type="text" name="location" class="inputBox" placeholder="Type here">
  <div class="displaybox"></div>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

It seems you would need to get the ID of each input box and each output box?

function showTypedInput(inputID, outputID) {
    var inputBox = document.getElementById(inputID);
    var outputBox = document.getElementById(outputID);
    inputBox.onkeyup = function(){
        outputBox.innerHTML = inputBox.value;
    };
}

Then you just reuse this? showTypedInput("myInputBox", "myOutputBox");

about 4 years ago · Juan Pablo Isaza Report

0

You can create this functionality using following:

function listener(target){
  return function(e){target.innerHTML = e.target.value};
}

function init(){
  var elems = document.querySelectorAll("input[data-keyuptarget]");
  for(var elem of elems){
    var target = document.getElementById(elem.getAttribute('data-keyuptarget'));
    if (target) elem.onkeyup = listener(target);
   }
 }

init();

In html just use

  <input type='text' name='name' data-keyuptarget="displayBox1">
  <div id='displayBox1'></div>


  <input type='text' name='name' data-keyuptarget="displayBox2">
  <div id='displayBox2'></div>

JS Bin : https://jsbin.com/piwiyapohe/edit?html,output

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!