Skip to content

Instantly share code, notes, and snippets.

@myanmarlinks
Created September 27, 2017 13:11
Show Gist options
  • Save myanmarlinks/f83b52f834118b511ba4b7bfac2080c8 to your computer and use it in GitHub Desktop.
Save myanmarlinks/f83b52f834118b511ba4b7bfac2080c8 to your computer and use it in GitHub Desktop.
Get Element By Class Name
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>DOM TEST</title>
</head>
<body>
<div id="container">
<div class="row">
<h1>Hello World</h1>
<a href="#">Click Here</a>
</div>
<div class="row">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum beatae quas praesentium. Distinctio hic vel nihil, eum facilis doloremque incidunt atque autem dolor est esse fugit tempora ea magni, quis.</p>
</div>
<div class="row">
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Odit, sunt provident et dicta, dolores, deleniti, sint unde assumenda nulla doloremque neque. Velit at praesentium cum delectus quae optio. Quisquam, veritatis.</p>
</div>
<div class="row">
<ul>
<li><a href="#">How are you</a></li>
<li><a href="#">How do you do</a></li>
<li><a href="#">What are you doing</a></li>
<li><a href="#">Where do you live</a></li>
</ul>
</div>
</div>
<script>
var container = document.getElementById("container");
container.style.backgroundColor = "#e3e3e3";
container.style.padding = "20px";
var myHeader = document.getElementsByTagName("h1");
console.log(myHeader);
myHeader[0].style.color = "red";
var myLinks = document.getElementsByTagName("a");
console.log(myLinks);
myLinks[1].style.color = "green";
var myRows = document.getElementsByClassName("row");
myRows[0].style.backgroundColor = "red";
myRows[1].style.backgroundColor = "green";
var myThirdRow = myRows[2];
var myThirdRowP = myThirdRow.getElementsByTagName("p");
myThirdRowP[0].style.color = "#673AB7";
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment