JavaScript JavaScript multiple event handler with same class input javascript multiple event listeners,javascript multiple event handlers,javascript multiple

I need to handle multiple event which will generate same HTML dynamically. I have added addEventListener for all elements. Also getting different event value. Now i just need to set this result to this obj child .display element.

const inputs = document.querySelectorAll('.input-box');
inputs.forEach((input) => {
  input.addEventListener('input', function(e) {
    //console.log(e.target.value);
    //Method 1
    e.target.parentNode.querySelector('.display').innerHTML = e.target.value;
    //Method 2
    //e.target.nextElementSibling.innerHTML = e.target.value;
  });

})
body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  width: 80%;
  margin: 0 auto;
  padding: 10px;
  overflow:auto;
  height: 100vh;
}

.input-box {
  width: 200px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Event Handeller</title>
</head>

<body>
  <div class="input-box">
    <p>Search Box 1</p>
    <input type="text">
    <div class="display">Search Box 1 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 2</p>
    <input type="text">
    <div class="display">Search Box 2 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 3</p>
    <input type="text">
    <div class="display">Search Box 3 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 4</p>
    <input type="text">
    <div class="display">Search Box 4 Result</div>
  </div>


</body>

</html>
Answer:1

You can target the parent and then use querySelector to target the sibling with class display

const inputs = document.querySelectorAll('.input-box');
inputs.forEach((input) => {
  input.addEventListener('input', function(e) {
    e.target.parentNode.querySelector('.display').innerHTML = e.target.value
  });

})
body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  width: 80%;
  margin: 0 auto;
  padding: 100px;
}

.input-box {
  width: 200px;
}
<div class="input-box">
  <p>Search Box 1</p>
  <input type="text">
  <div class="display">Search Box 1 Result</div>
</div>
<div class="input-box">
  <p>Search Box 2</p>
  <input type="text">
  <div class="display">Search Box 2 Result</div>
</div>
<div class="input-box">
  <p>Search Box 3</p>
  <input type="text">
  <div class="display">Search Box 3 Result</div>
</div>
<div class="input-box">
  <p>Search Box 4</p>
  <input type="text">
  <div class="display">Search Box 4 Result</div>
</div>
Answer:2

You can first get the parentNode and then use querySelector()

e.target.parentNode.querySelector('.display').innerHTML = e.target.value;

Or another way is to use nextElementSibling

e.target.nextElementSibling.innerHTML = e.target.value;

const inputs = document.querySelectorAll('.input-box');
inputs.forEach((input) => {
  input.addEventListener('input', function(e) {
    e.target.nextElementSibling.innerHTML = e.target.value;
  });

})
body {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  width: 80%;
  margin: 0 auto;
  padding: 10px;
  overflow:auto;
  height: 100vh;
}

.input-box {
  width: 200px;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Event Handeller</title>
</head>

<body>
  <div class="input-box">
    <p>Search Box 1</p>
    <input type="text">
    <div class="display">Search Box 1 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 2</p>
    <input type="text">
    <div class="display">Search Box 2 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 3</p>
    <input type="text">
    <div class="display">Search Box 3 Result</div>
  </div>
  <div class="input-box">
    <p>Search Box 4</p>
    <input type="text">
    <div class="display">Search Box 4 Result</div>
  </div>


</body>

</html>
Answer:3

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...

Following scenario/my solution consists of the following:  Project one: (SELF HOST) I have a SignalR console application which handles the logic including the authentication process ( queries ...

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I'm trying to solve the task, get quantity of missed elements from an array. For example, if given an array [1,3,6], the quantity of missed elements is 3 (2,4,5). But somewhere code goes wrong and ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

I make a node js web app for generating report. My idea is to use .jasper file (jasper report) to generate these reports. I've tried a bunch of node js library to do this, but nothing seems to work. ...

  1. file with node
  2. file upload with node js
  3. download file with node
  4. run file with node
  5. read file with node
  6. create file with node
  7. download file with node js
  8. read file with node js
  9. create file with node js
  10. open file with node.js
  11. delete file with node js
  12. write file with node js
  13. write file with node
  14. delete file with node
  15. upload file with node
  16. file upload with node and react
  17. open file with node
  18. file server with node.js
  19. node dockerfile
  20. send file with node js

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...

As mentioned in the interface section of 'Programming javascript applications' you can implement interfaces with stampit. When reducing the unnecessary stuff from the example, I end up with something ...