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

321
Visualizações
How to get values of multiple input fields by ID, separately

so I have a form which has 4 blocks of results at the end, the results are based on answers selected by the user. What I want to do is get the values of all the blocks individually( I have tried doing this via the DOM but I observed the value attribute is blank for some reason) and compare all of them to find the largest value, which I will use for some logic that I want to implement on the results page.

I have tried using the code below to achieve that, however there is some iteration going on and I am not sure why.I have declared an array for the values and logged it to the console for clarity.

I am still not proficient in JavaScript, so please pardon me.Please kindly guide me in the redirection.

    // get values of the CAT scores from the DOM & compare to get largest value
$('#field_wyepk, #field_wyepk2, #field_wyepk3 , #field_wyepk32').change(function(){
  var commerceScore = $("#field_wyepk").val();
  var scitechScore = $("#field_wyepk2").val();
  var humanitiesScore = $("#field_wyepk3").val();
  var eduScore = $("#field_wyepk32").val();
  
console.log(commerceScore);
console.log(scitechScore);
console.log(humanitiesScore);
console.log(eduScore);

var array = [commerceScore,scitechScore, humanitiesScore,eduScore];
      console.log(array);

      var largest= 0;

       for (i=0; i<=largest; i++){
        if (array[i]>largest && array[i] !== ""){
        var largest=array[i];
          }
        }
        
});

Please see attached snippet of the console resultsCONSOLE RESULTS SNIPPET

All I need is to get an array with [71, 85, 76, 56] as an example using the snippet and I will do the rest.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As was said in the comment, you have a change listener listening for changes to all of the results. Any time one of the results is changed, all of them are added to your array and logged to the console. Maybe a change listener isn't what you need - perhaps you could get the user to click a button when all the results have been entered, then on the button click create the array. Otherwise, you could parse your array and check that all the values have a length?

const validArray = !array.some(item => item.length === 0);

(will return false if your array contains one or more instances of empty string, or true if your array is valid. Only proceed to the next step if the value is true)

Also note that your array is made up of strings, not integers. I'm not sure if this is what you want.

about 4 years ago · Juan Pablo Isaza 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