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

201
Views
¿Cómo establecer atributos de múltiples elementos en HTML usando una matriz?

Estoy tratando de ocultar varias secciones en mi documento HTML en función del clic del botón, excepto una. Sin embargo, recibí un error de la siguiente manera:

 Uncaught TypeError: arr[i].setAttribute is not a function

Mi fragmento de código se ve a continuación;

 const hideOthers = (keepSec) => { let allSec = ["1Sec", "2Sec", "3Sec", "4Sec", "5Sec", "6Sec", "7Sec"]; arr = allSec.filter((e) => e !== keepSec); console.log(arr); // arr.forEach(element => { // element.setAttribute("hidden", true); // }); for (var i = 0; i < arr.length; i++) { arr[i].setAttribute("hidden", true); } };

Antes, probé usando el camino largo y funciona bien. Fue poniendo varias líneas de setAttribute como a continuación:

 1Sec.setAttribute("hidden", true); 2Sec.setAttribute("hidden", true); 3Sec.setAttribute("hidden", true);

¿Cómo minimizo la codificación y quizás ejecuto la función en función de los datos proporcionados por la matriz? Cualquier ayuda es muy apreciada

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const Sec1 = document.querySelector('.Sec1'); const Sec2 = document.querySelector('.Sec2'); const hideOthers = (keepSec) =>{ let allSec = [{name: 'Sec1', obj: Sec1 }, {name: 'Sec2', obj: Sec2}]; arr = allSec.filter(e => e.name !== keepSec); for(var i = 0; i< arr.length; i++){ arr[i].obj.setAttribute("hidden", true); } } hideOthers();

El mismo otro comentario anterior, su matriz es una cadena [], no un objeto DOM, por lo que no puede establecer el Atributo ()
Mi solución es que uso un objeto de matriz para almacenar con: nombre para ordenar, obj para hacer setAttribute.

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!