Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

261
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda