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

197
Vistas
document.getElementByClassName does not work to get child element's value using parents class

basically I made this php & html code to print out some stuff from the database related to products ( it is in a loop, so it prints out all those products in the database with different ids and values )

                     echo '
                     <div class="col-md-9 col-sm-9 '.$results_two['id'].'">
                     <h3>'.$results_two['name'].' ................ <span>$'.$results_two['price'].'</span></h3>
                     <h4><h4>'.$results_two['descr'].'</h4>  <button type="button" onClick="delete_product(this.id)" class="btn btn-danger" id="'.$results_two['id'].'" style=" height:30px;">Delete</button><button type="button" class="btn btn-warning" id = "'.$results_two['id'].'" style="height:30px; margin-left:3px;" onClick="edit_product(this.id)">Edit</button></h4>
                     </div>
                     ';

as you can see I have created delete & edit buttons to modify and remove information from database.
delete button works perfectly fine, just as I expected, but edit button makes me wanna shoot myself... been trying to figure this out for an hour and half and I'm pretty sure it's gonna be a stupid mistake.

here's js code that is being triggered once edit button is pressed in html

               function edit_product(id_clicked){
               var x = document.getElementsByClassName(id_clicked).textContent;
               var value = x[0].textContent;
               alert(value);
            };

product id and parent div class are both exactly the same, because I want to use it as a parent element to get value of those h3, h4, span elements.
what I want this to do is to get product id from html side as a paramater in function and use it to determine children of div element and later get value of those ( please suggest if there is an easier way for doing it, I would really appreciate it )
but what I get instead is that it's not being alerted at all, however when I try to alert the document.getElementsByClassName(id_clicked) itself, it shows this - [object HTMLCollection]
thanks you in advance for helping a beginner out.

about 4 years ago · Juan Pablo Isaza
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