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

97
Vistas
How to display HTML of getElementsByClassName with JSON.parse()

How to I adjust this code to display the value or favFood? Works with getElementById but not getElementsByClassName but I want to use a class:

server file - ProcesssingFileOnServer.php:

<?php

$myObj = new stdClass();
$myObj->name = "John"; 
$myObj->favFood = "Pizza";

$myJSON = json_encode($myObj);

echo $myJSON;

?>

client file:

<!DOCTYPE html>
<html>
<head>

</head>
<body>    

<p id="theName"></p>
<div class="bla"></div>

<script>    
const xmlhttp = new XMLHttpRequest();

xmlhttp.onload = function() {
  const myObj = JSON.parse(this.responseText);

  document.getElementById("theName").innerHTML = myObj.name;
  document.getElementsByClassName("bla") = myObj.favFood;
}
xmlhttp.open("GET", "ProcesssingFileOnServer.php");
xmlhttp.send();
</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When they use the class, the result is a array.

var el = document.getElementsByClassName("bla")
for (let i = 0; i < el.length; i++) {
    el[i].innerHTML = 'asdsadas';
    
}
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