How to Get Value by Class Name using JavaScript ? Last Updated : 05 Aug, 2025 Comments Improve Suggest changes Like Article Like Report This article will show you how to use JavaScript to get value by class name. To get the value of an element by its class name in JavaScript, you can use the getElementsByClassName() method. This method returns an array-like object of all elements with the specified class name. You can then access the value property of the first element in the array to get the value. Example: In this example, we will get the classname of an element. HTML <!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"> GeeksforGeeks </h1> <p class="para"> 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('heading'); // 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[0].innerHTML; // Update the content of the 'result' element document.getElementById('result') .innerHTML = headingVal; } else { console.log( 'Element with class name "heading" not found.'); } } </script> </body> </html> Output: Example 2: In this example, we will get the second element by class name. HTML <!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> Output: Create Quiz Comment V vkash8574 Follow 0 Improve V vkash8574 Follow 0 Improve Article Tags : JavaScript Web Technologies Geeks Premier League JavaScript-Questions Geeks Premier League 2023 +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings5 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in JavaScript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)8 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readScope of Variables in JavaScript3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like