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

183
Visualizações
How to call getElementById() on multiple ids

Previously I made an onchange input, there are several inputs that have the same thing called by id. How do I call multiple ids in one line of code? I tried to write it like this but failed:

   
function minmax() {
  const inp_field = document.querySelectorAll("#input1, #input2, #input3, #input4");

  let val = parseFloat(inp_field.value),
      min = parseFloat(inp_field.min),
      max = parseFloat(inp_field.max);

  if (val < min) {
      inp_field.value = min;
  } else if (val > max) {
      inp_field.value = max;
  } else {
      inp_field.value = val;
  }
}
<input id="input1" type="text" onchange="minmax()" min="0.01" max="94.99">
<input id="input2" type="text" onchange="minmax()" min="0.1" max="99.99">
<input id="input3" type="text" onchange="minmax()" min="5" max="25">
<input id="input4" type="text" onchange="minmax()" min="1" max="10">

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

Assuming that you want to update each input value based on its min/max attributes as it changes:

  1. There's no real need for those ids. If you wanted to group them you could use a class instead, but here I'm just picking up the inputs by element name.
  2. Remove the requirement for inline JS and use addEventListener to attach listeners to each input.
  3. You can use this (the context referring to the clicked element) in the function to make the code more concise.
  4. You can remove this.value = val since the input value is already set.
  5. Finally, while max/min aren't specified as attributes on a you can use with type="text" apparently you can still use them. But you may want to change to type="number".

const inputs = document.querySelectorAll('input');

inputs.forEach(input => {
  input.addEventListener('change', minMax);
});

function minMax() {

  const val = parseFloat(this.value);
  const min = parseFloat(this.min);
  const max = parseFloat(this.max);

  if (val < min) this.value = min;
  if (val > max) this.value = max;

}
<input type="text" min="0.01" max="94.99">
<input type="text" min="0.1" max="99.99">
<input type="text" min="5" max="25">
<input type="text" min="1" max="10">

about 4 years ago · Santiago Gelvez Relatório

0

If your aim is to keep all the inputs in sync whenever one of them is changing, then a slight adjustment is required using forEach. Keep in mind, however, that querySelectorAll produces a NodeList, not an Array:

 function minmax() {
  const inp_fields = document.querySelectorAll("#input1, #input2, #input3, #input4");

  inp_fields.forEach(inp_field => {
    let val = parseFloat(inp_field.value),
    min = parseFloat(inp_field.min),
    max = parseFloat(inp_field.max);

    if (val < min) {
        inp_field.value = min;
    } else if (val > max) {
        inp_field.value = max;
    } else {
        inp_field.value = val;
    }
  });
}

Just make sure to change your input fields type to number and set a proper default value (for example, 0), because otherwise your calculations will yield to NaN being set to all the other fields upon modifying any of them.

about 4 years ago · Santiago Gelvez Relatório

0

I doubt you really need all that... try this and see if it serves your need.

function minmax(inp_field) {

    let val = parseFloat(inp_field.value),
    min = parseFloat(inp_field.min),
    max = parseFloat(inp_field.max);

    //below we call a function that checks if the value contains any scientific/engineering notation
    val = decimalCount(val)

    if (val < min) {
        inp_field.value = inp_field.min;
    } else if (val > max) {
        inp_field.value = inp_field.max;
    } else {
        inp_field.value = val;
    }
}

function decimalCount(val){
    let valStr = val.toString(); //convert float to string
    //check if the value include e-... example 1e-7
    if(valStr.includes('e-')){
        //get the value before the scietific notation
        let beforeE = valStr.substr(0,valStr.indexOf('e'));
        //the following removes a comma. example 0.000000017 == 1.7e-7
        beforeE = beforeE.replace('.','')
        //get the number of zeros after the scientific notation
        let decimalPlace = valStr.substr((valStr.indexOf('-') + 1)) - 1
        //we set a variable for the zeros after the .
        let zeros = '.';
        //assign the zeros to appear after the .
        for(let i=0;i<decimalPlace;i++){
            zeros += 0;
        }
        //concatenate the results and return the value for display
        return 0 + zeros + beforeE;
    }else{
        //else, we return the min/max as it is
        return val;
    }
}
<input id="input1" type="text" onchange="minmax(this)" min="0.01" max="94.99">
<input id="input2" type="text" onchange="minmax(this)" min="0.00000001" max="99.99">
<input id="input3" type="text" onchange="minmax(this)" min="5" max="25">
<input id="input4" type="text" onchange="minmax(this)" min="1" max="10">

about 4 years ago · Santiago Gelvez 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