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

194
Visualizações
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 à 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