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

86
Visualizações
Struggling to show data from javascript in html

Note: I am new to JavaScript and html so there are a lot of things i do not quite understand yet.

I am trying to make a web-application that uses a bus-schedule API to show the bus times around my apartment. I have managed to retrieve the data, but I struggle to display that data in the html. (edit: it is possibly a SDK, i dont really know the difference)

// this is the function write_bus() in my javascript file departures.js:
function write_bus() {
  document.getElementById("bustime-kong").innerHTML = data;
}

// 'data' is a variable in javascript that contains the information I fetched from the client, and is what I am trying to show.It is on the following format:
const data = {
  aimedArrivalTime: '2022-01-06T12:36:00+0100',
  aimedDepartureTime: '2022-01-06T12:36:00+0100',
  cancellation: false,
  date: '2022-01-06',
  destinationDisplay: {
    frontText: 'xxx'
  },
  expectedDepartureTime: '2022-01-06T12:38:12+0100',
  expectedArrivalTime: '2022-01-06T12:37:32+0100',
  forAlighting: true,
  forBoarding: true,
  notices: [],
  predictionInaccurate: false,
  quay: {
    id: 'xxx',
    name: 'xxx',
    publicCode: 'P2',
    situations: [],
    stopPlace: [Object]
  }
}
<script src="departures.js"></script>
<input type="button" onclick="write_bus()" value="Busstider"> <br>
<div id="bustime-kong"></div>

(In order to save some space i removed 2/3 of the data)

I appreciate every bit of help i could get!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Since it is array of data you need to use Json.stringify method,

document.getElementById("bustime-kong").innerHTML = JSON.stringify(data);
about 4 years ago · Juan Pablo Isaza Relatório

0

You can't insert raw objects like that as DOM text. You have to use specific properties, but if you are so inclined to insert object like that, use JSON.stringify() method.

document.getElementById("bustime-kong").innerHTML = JSON.stringify(data);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

BTW, It is good practice to put <script> tag at the end to make sure HTML is loaded before running any JavaScript.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a function that will parse through the json and print it in table form like below.

// this is the function write_bus() in my javascript file departures.js:
function write_bus() {
  document.getElementById("bustime-kong").innerHTML = createTable(data);
}

// 'data' is a variable in javascript that contains the information I fetched from the client, and is what I am trying to show.It is on the following format:
const data = {
  aimedArrivalTime: '2022-01-06T12:36:00+0100',
  aimedDepartureTime: '2022-01-06T12:36:00+0100',
  cancellation: false,
  date: '2022-01-06',
  destinationDisplay: {
    frontText: 'xxx'
  },
  expectedDepartureTime: '2022-01-06T12:38:12+0100',
  expectedArrivalTime: '2022-01-06T12:37:32+0100',
  forAlighting: true,
  forBoarding: true,
  notices: [],
  predictionInaccurate: false,
  quay: {
    id: 'xxx',
    name: 'xxx',
    publicCode: 'P2',
    situations: [],
    stopPlace: [Object]
  }
}

function createTable(data){

  var table = "<table>";
  for(var key in data){
    table+="<tr>";
    table+="<td>"+key+"</td>";
    table+="<td>"+data[key]+"</td>";
    table+="</tr>";
  }
  table+="</table>";
  return table;
}
<input type="button" onclick="write_bus()" value="Busstider"> <br>
<div id="bustime-kong"></div>

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