All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Get Child Element By Class

Last Updated : Mar 11, 2024

JavaScript Get Child Element By Class

In this tutorial we will show you the solution of JavaScript get child element by class, mainly there are many methods with the help of which we are able to get child element by class.

Let us understand them one by one.

Step By Step Guide On JavaScript Get Child Element By Class :-

As, here below there are some methods which we are going to show in codes. Let see the codes and we will also discuss codes step by step.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>home page</title>
</head>
<body>
 <ul id="parent-here">
      <li class="child-1">
        First child
      </li>
      <li class="child-2">
        Second child
      </li>
      <li class="child-3">
        Third child
      </li>
      <li class="child-4">
        Fourth Child
      </li>
      <li class="child-5">
        Fifth Child
      </li>
  </ul>
 <script>
    var parent = document.querySelector('#parent-here');
    var firstchild = document.querySelector('.child-1');
    var lastchild = document.querySelector('.child-5');
    document.writeln("The innerHTML of First Child Element is \"" +
      firstchild.innerHTML + "\"" +
      " and The innerHTML of Last Child Element is \"" +
      lastchild.innerHTML + "\"");
    document.write("<br>");
    var parent = document.getElementById('parent-here');
    var allChildElements = parent.querySelectorAll('.child-element');
    for (i = 0; i < allChildElements.length; i++)
      document.writeln(allChildElements[i].innerHTML);
    document.write("<br>");
    var parent = document.getElementById('parent-here');
    var firstChild = parent.firstElementChild;
    var lastChild = parent.lastElementChild;
    document.writeln("The innerHTML of First Child Element is \"" +
      firstChild.innerHTML + "\"" +
      " and The innerHTML of Last Child Element is \"" +
      lastChild.innerHTML + "\"");
    document.write("<br>");
    var parent = document.getElementById('parent-here');
    var allChild = parent.children;
    for (i = 0; i < allChild.length; i++)
      document.writeln(allChild[i].innerHTML);
    document.write("<br>");
    var childs = (document.getElementById('parent-here')).
      getElementsByClassName('child-element');
    for (i = 0; i < childs.length; i++)
      document.writeln(childs[i].innerHTML);
  </script>
</body>
</html>
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Here, then we create a body tag and inside body tag, we use ul and li’s to create a list.
  6. Inside list ul tag acts as parent tag and rest li’s are as child tag. You can see the above code for reference.
  7. Now, inside body after html content, we use JavaScript code here. And as we know the code is written always under script tag which is a paired tag. Rest we use some JavaScript code to get parent, child and use document.writeln to print output, etc.
  8. If you don’t know about basic codes of JavaScript and its basic queries then you must visit to our article in which we describe basics of JavaScript.
  9. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with help of this article we are able to get child element by class in different ways.

I hope this tutorial on JavaScript get child element by class helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪