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

221
Views
Javascript - How do I pick up which element fired an event

Using the sample code from W3schools I am trying to set up a single event handler that will fire when any of multiple 'input' elements in a form are changed.

What I want is to receive the <input name=xxxx> name parameter As I understand it, p1 and p2 should contain the event and the name of the <input name=xxxx> field that fired the event. Instead I get a reference error, "p1 is not defined" from the event handler. On search I have found many convoluted solutions to this question, some of which I don't understand, but none that actually solve my problem. What am I doing wrong?

function field_check(event) {
  var the_input = event.target.id;

  alert("field_check: event was: " + event + "\nand the entry field was: " + the_input);
}

document.getElementById("add_data").addEventListener("change", field_check);
input {
  background-color: lightblue;
}

input:focus {
  background-color: yellow;
}
<form name=add_data id=add_data>
  <input type="text" class="ship" name="fname" id="fname" autofocus><br \>
  <input type="text" class="ship" name="lname" id="lname" required><br />
  <input type="text" class="ship" name="addr1" id="addr1"><br />
  <input type="text" class="ship" name="addr2" id="addr2"><br />
  <input type="text" class="ship" name="city" id="city"><br />
  <input type="text" class="ship" name="p_code" id="p_code">
</form>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You mean to do

var s = document.getElementById("add_data");
s.addEventListener("change", function(event) {
  field_check(event, event.target.name);
});

or even simpler

document.getElementById("add_data").addEventListener("change", field_check);

and have

function field_check(event) {
  console.log(event.target.name);
}

function field_check(event) {
  console.log(event.target.name);
}

document.getElementById("add_data").addEventListener("change", field_check);
input {
  background-color: lightblue;
}

input:focus {
  background-color: yellow;
}
<form name=add_data id=add_data>
  <input type="text" class="ship" name="fname" id="fname" autofocus><br \>
  <input type="text" class="ship" name="lname" id="lname" required><br />
  <input type="text" class="ship" name="addr1" id="addr1"><br />
  <input type="text" class="ship" name="addr2" id="addr2"><br />
  <input type="text" class="ship" name="city" id="city"><br />
  <input type="text" class="ship" name="p_code" id="p_code">
</form>

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!