Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
¿Cómo llamar a múltiples ID en una función?

Encontré esta función que me permite hacer lo que quiero, pero como tengo muchas entradas y no quiero usar este JS en todas ellas, me gustaría llamarlo solo en un par de entradas. ¿Puedo hacer eso por identificación o algo?

(Sé que esta frase no es de las mejores, pero tengo prisa, lo siento)

 var pastValue, pastSelectionStart, pastSelectionEnd; $("input").on("keydown", function() { pastValue = this.value; pastSelectionStart = this.selectionStart; pastSelectionEnd = this.selectionEnd; }).on("input propertychange", function() { var regex = /^(100|(\d|[1-9]\d)(\.\d{0,2})?)$/; if (this.value.length > 0 && !regex.test(this.value)) { this.value = pastValue; this.selectionStart = pastSelectionStart; this.selectionEnd = pastSelectionEnd; } });
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Publicaré una alternativa jQuery, ya que ha publicado jQuery en su código de pregunta.

Simplemente cambia el selector de CSS por un elemento de entrada con el selector de CSS por sus ID de elementos específicos, así:

 var pastValue, pastSelectionStart, pastSelectionEnd; $("#myID1, #myID2, #myID3").on("keydown", function() { pastValue = this.value; pastSelectionStart = this.selectionStart; pastSelectionEnd = this.selectionEnd; }).on("input propertychange", function() { var regex = /^(100|(\d|[1-9]\d)(\.\d{0,2})?)$/; if (this.value.length > 0 && !regex.test(this.value)) { this.value = pastValue; this.selectionStart = pastSelectionStart; this.selectionEnd = pastSelectionEnd; } });
about 4 years ago · Juan Pablo Isaza Denunciar

0

En realidad, establecer la clase en cada entrada a la que desea acceder ayudaría, luego acceder a ellos usando getElementsByClassName y luego agregará su detector de eventos a todas las entradas necesarias.

 const myInputs = document.getElementsByClassName("myInputs"); // consider myInputs as the class I gave to wanted inputs myInputs.forEach(input=>{ input.addEventListener('click', functionHandler); // functionHandler is the function where you will put your logic })

Espero que esto va a ser útil

about 4 years ago · Juan Pablo Isaza Denunciar

0

Dale la clase .inputTest a todas tus entradas

 var inputs = $(".inputTest"); for (var obj of inputs) { var pastValue, pastSelectionStart, pastSelectionEnd; obj .on("keydown", function () { pastValue = this.value; pastSelectionStart = this.selectionStart; pastSelectionEnd = this.selectionEnd; }) .on("input propertychange", function () { var regex = /^(100|(\d|[1-9]\d)(\.\d{0,2})?)$/; if (this.value.length > 0 && !regex.test(this.value)) { this.value = pastValue; this.selectionStart = pastSelectionStart; this.selectionEnd = pastSelectionEnd; } }); }

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda