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

143
Views
el DOM no funciona conmigo cuando trato de ordenar un elemento

aquí está el archivo que tengo. cuando trato de ordenar algún número, no cambia y se mantiene vacío

archivo HTML

 <tr> <th>Original array</th> <td id ="td1">1,5,7,-1,3</td></tr> <tr> <th>Sorted Array</th> <td id="td2"></td>

archivo JS

 var OriginalArr = document.getElementById("td1").value; var a = document.getElementById("td2").value; a.value = Original.sort();
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hay dos problemas principales con esto. En primer lugar, la mayoría de los elementos no tienen una propiedad de value , solo los elementos que toman entrada. Parece que está buscando textContent en este contexto.

Otra cosa es que cuando obtiene el texto en td1 , debe convertirse en una matriz antes de poder ordenarlo.

Aquí está el código con los problemas mencionados solucionados:

 // Get text in td1 var td1Text = document.getElementById("td1").textContent; // Get array of numbers from text var OriginalArray = td1Text.split(",").map(function(num){ return parseInt(num,10); }); // Get td2 var td2 = document.getElementById("td2"); // Set td2 to sorted array converted to string td2.textContent = OriginalArray.sort().toString();
 <table> <tr> <th>Original array</th> <td id ="td1">1,5,7,-1,3</td></tr> <tr> <th>Sorted Array</th> <td id="td2"></td></tr> </table>

Una cosa más a tener en cuenta es que las etiquetas <tr> deben estar en una <table> para que getElementById las consulte correctamente. Sin la etiqueta de la tabla, getElementById devuelve null .

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!