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

259
Views
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 answers
Answer question

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 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!