Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda