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

171
Views
¿Cómo agregar una función en js a un elemento html, que solo se hizo en js?

Me gustaría cambiar el color de fondo de una fila en una tabla, si selecciono un objeto de la lista de "selección", pero solo si este elemento, que es un número, es par. Pero el problema principal es que el "objeto" está hecho en js, por lo que no está en el archivo html, por lo que no pude agregar a los objetos una función de clic, lo que podría hacer que cambie el color de fondo de la fila. Por favor ayuda, he perdido.

HTML:

 <select class = "form-select, col-6" id="quantiti" onclick = "ifSelectedLetHeadChange()" > <!-- This was the original version that I copied: <select class="form-select, col-6" id="quantiti" onclick="ifSelectedLetHeadChange($(this).val())"> --> </select>

JS:

 function quantity(amount) { var select = document.getElementById('quantiti'); for (var i = 0; i < amount; i++) { select.options[select.options.length] = new Option(i + 1, i); select.options[select.options.index] = i+1 ; select.options[select.options.onclick]="ifSelectedOptionLetTrChange()"; } } quantity(10000); function ifSelectedOptionLetTrChange(value) { if (option.value % 2 ) { $("tr").css("background-color", "lightblue"); } else { $("tr").css("background-color", "blue"); } }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Parece que estás trabajando con JQuery. Puedes hacerlo con JS simple o con JQuery.

JQuery

Se recomienda trabajar con el evento de cambio en lugar de un evento de clic. Sin embargo, el evento de cambio solo se activará en un cambio. Lo haré con el cambio por esto.

 var select = $("#quantiti"); var tr = $("tr"); select.on('change', function() { var value = $(this).val(); setTrColor(value); }); function setTrColor(value) { tr.css("background-color", value % 2 ? "lightblue" : "blue"); }

JavaScript

 var select = document.getElementsById("quantiti"); var tr = Array.prototype.slice.call(document.getElementsByTagName("tr")); select.addEventListener("change", function(event) { var targetElement = event.target || event.srcElement; var value = targetElement.value; setTrColor(value); }); function setTrColor(value) { tr.forEach(function(element){ element.style.backgroundColor = value % 2 ? "lightblue" : "blue"; }); }
about 4 years ago · Juan Pablo Isaza 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!