• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Get the First Child Element and Change it with JavaScript

Get the First Child Element and Change it with JavaScript

January 30, 2022 Leave a Comment

To get the first child of an HTML element, we can use the firstElementChild property and use JavaScript to make changes to that element.

document.getElementById("div1").firstElementChild;

Let’s say we have the following HTML:

<div id="div1">
   <p>This is the first child of #div1</p>
   <p>This is the second child of #div1</p>
   <p>This is the third child of #div1</p>
   <p>This is the fourth child of #div1</p>
</div>

To get the first paragraph child of the div, we can use the firstElementChild property and use the following JavaScript code:

document.getElementById("div1").firstElementChild.style.backgroundColor = "#c1e9c1";

How to Get the First Child on Click using JavaScript

We can get the first child of an HTML element using JavaScript very easily by combining the firstElementChild property with a click event.

Let’s say we have the following HTML code and we want to change the background color of the first paragraph.

<div id="div1">
  <p>This is paragraph 1</p>
  <p>This is paragraph 2</p>
  <p>This is paragraph 3</p>
</div>
<div id="click-me" onclick="changeBG()">Change background</div>

We can utilize the firstElementChild property, and the backgroundColor property to change the background of the first paragraph.

Below is the JavaScript code which will allow the user to be able to select the first paragraph and set the new background color.

function changeBG() {
  document.getElementById("div1").firstElementChild.style.backgroundColor = "#c1e9c1";
}

The final code and output for this example is below:

Code Output:

This is paragraph 1

This is paragraph 2

This is paragraph 3

Change background

Full Code:

<div id="div1">
  <p>This is paragraph 1</p>
  <p>This is paragraph 2</p>
  <p>This is paragraph 3</p>
</div>
<div id="click-me" onclick="changeBG()">Change background</div>

<script>

function changeBG() {
  document.getElementById("div1").firstElementChild.style.backgroundColor = "#c1e9c1";
}

</script>

Hopefully this article has been useful to help you understand how to get the first child element and change it with JavaScript

Other Articles You'll Also Like:

  • 1.  JavaScript asinh – Find Hyperbolic Arcsine of Number Using Math.asinh()
  • 2.  JavaScript substring – How to Get a Substring From a String
  • 3.  pi JavaScript – Get Value of pi from JavaScript Math Module
  • 4.  Using JavaScript to Check if String Contains Letters
  • 5.  How to Check if a Number is Even or Odd in JavaScript
  • 6.  How to Find the Longest String in an Array in JavaScript
  • 7.  Using JavaScript to Create an Empty Array
  • 8.  Using JavaScript to Get Substring Between Two Characters
  • 9.  Using JavaScript to Get a Random Number Between 1 and 5
  • 10.  Remove Brackets from String Using JavaScript

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy