<!DOCTYPE html>
<
html
lang
=
"en"
>
<
head
>
<
meta
charset
=
"UTF-8"
>
<
meta
name
=
"viewport"
content
=
"width=device-width, initial-scale=1.0"
>
<
title
>Get Value by Class Name</
title
>
</
head
>
<
body
style
=
"text-align: center;"
>
<
h1
class
=
"heading text"
>
GeeksforGeeks
</
h1
>
<
p
class
=
"para text"
>
A computer science portal for geeks
</
p
>
<
button
onclick
=
"myGeeks()"
>
Get Value
</
button
>
<
p
id
=
"result"
></
p
>
<
script
>
function myGeeks() {
// Get elements with the class name 'heading'
let headingElements =
document.getElementsByClassName('text');
// Check if any elements with the
// specified class name exist
if (headingElements.length > 0) {
// Access the value of the first
// element in the collection
let headingVal = headingElements[1].innerHTML;
// Update the content of the 'result' element
document.getElementById('result')
.innerHTML = headingVal;
} else {
console.log(
'Element with class name "text" not found.');
}
}
</
script
>
</
body
>
</
html
>