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

256
Vistas
How to show json in div?

I have two buttons SHOW and HIDE. When show is clicked numbers will appear (which are json files, each json file contain only one number 1, 2, 3..) when HIDE is clicked first number(json) in a row dissapear. For example we clicked SHOW button 3 times and got this: 1 2 3 and then clicked HIDE once then we got: 2 3 shown. My problem is when HIDE is clicked I want to save that hidden number by showing it in my div where id="nome". After another clicking on button HIDE another hidden number is shown and old is deleted from div. I tried this:

var pageCounter = 1;
var pageCounterr = 1;
var animalContainer = document.getElementById("animal-info");
var animalContainerr = document.getElementById("nome");
var btn = document.getElementById("btn");
var btnn = document.getElementById("btnn");

btn.addEventListener("click", function(){

var ourRequest = new XMLHttpRequest();
ourRequest.open('GET','http://10.0.9.243/animals-'+ pageCounter + '.json');
ourRequest.onload = function(){
  var ourData = JSON.parse(ourRequest.responseText);
  renderHTML(ourData);
};
ourRequest.send();
pageCounter++;
});

function renderHTML(data) {
  var htmlString  = document.createElement("div");
  for (i=0; i < data.length; i++){
    let pText = document.createTextNode(data[i].name);
    let pElement = document.createElement("p");
    pElement.append(pText);
    htmlString.append(pElement);
    htmlString.classList.add('containers') // new Line added
    }
  animalContainer.append(htmlString);
  
  }
  
  btnn.addEventListener("click", function(){
  let containers = document.getElementsByClassName('containers');
  if(containers.length > 0){
    document.getElementById("nome").innerHTML = containers[0];
      containers[0].remove();
  }
    });
<button id="btn">SHOW</button>
<button id="btnn">HIDE</button>
<h2>Numbers:</h2>
<div id="animal-info"></div>

<h2>Hidden number is:</h2>
<div id="nome"></div>

And what I get as a result is ' [object HTMLDivElement] ' in div with id="nome".

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The only thing that is wrong is the used logic, look this:

When you get elements by class name "container" you are getting a array of this: var htmlString = document.createElement("div");

Cause you defined that element's class to: htmlString.classList.add('containers')

Then you're just getting div.

Solution: Inside of this element you have a "p" element, and inside it you have a textNode with the content that you want!

Simple, good programming for you.

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