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

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