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

186
Views
Error while passing id of field as variable for javascript function

I have multiple forms created using for loop and I'm passing the field Id which is unique for all forms and fields to the javascript method but it is not reading the value using id.

Any other way to send the id so that I can get the exact field value in the javascript for ajax processing. Any help would be much appreciable.

<%
  String js = "Id" + count;
%>

<input name='Id' label=""  id='Id<%=count%>'  style="width:100px;" onChange="javascript:getName('<%=js %>')" />
function getName(count) {
  String field = count;
  var field1 = document.getElementById(field).value;
  alert(field1);
}

Whereas field1 is showing null

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

First of all the following code String field = count; is incorrect you need to replace String by var because this is how JavaScript variables declartion work, but anyway you can get rid of this line.

And your JS will be:

function getName(count) {
  var field1 = document.getElementById(count).value;
  alert(field1);
}

Optimisation:

Note that you can pass the id directly in your JSP like this:

<input name='Id' label=""  id='Id<%=count%>'  style="width:100px;" onChange="javascript:getName(this.value)" />

And in your JS:

function getName(count) {
   alert(count);
}
about 4 years ago · Santiago Trujillo Report

0

You need to do it like this

function getName(count) {
 // String field = count;
 var field = count;
 var field1 = document.getElementById(field).value;
 alert(field1);
 }

Javascript supports creating variables by using the var keyword not the String.

about 4 years ago · Santiago Trujillo Report

0

You can use

 onChange="javascript:getName(this)"

And in javascript

function getName(ref) {
    var id= ref.id;
    alert(id);
  }
about 4 years ago · Santiago Trujillo 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!