How to Get Value by Class Name using JavaScript ? Last Updated : 27 Dec, 2023 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: Comment More infoAdvertise with us Next Article How to Get Value by Class Name using JavaScript ? V vkash8574 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League JavaScript-Questions Geeks Premier League 2023 +1 More Similar Reads How To Get Element By Class Name In JavaScript ? When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript. 3 min read How to remove a Class name from an Element using JavaScript ? Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy 3 min read How to Get Cookie by Name in JavaScript? Getting a specific name in JavaScript involves parsing the document's cookie string contains all cookies in a single string separated by semicolons and space. The goal is to extract the value of a specific cookie by the name given. The Cookie is defined as a small piece of data that is stored on the 4 min read How to get the Class Name of an Object in JavaScript In this article, we will learn how we can get the class Name of an Object with multiple approaches in JavaScript. In JavaScript, determining an object's class name can be done using multiple approaches. The constructor property of an object reveals the function that created it, while the instanceof 3 min read How to get the month name from Date using JavaScript ? The purpose of this article is to get the month name from a particular date using JavaScript getMonth() method. This method is used to fetch the month(0 to 11) from the given Date object. It returns an integer value ranging from (0 to 11) Zero (0) means January, 1 means February, and so on, till 11 1 min read How to get the native type of a value in JavaScript ? JavaScript variables can store any type of value. The JavaScript typeof operator can be used to find out the native type of value. It returns a string value indicating the type of the value. Syntax: typeof(value) Possible output values of typeof: TypeResultundefined"undefined"null"object"boolean"boo 1 min read How to Filter a DIV Element Based on its Class Name using JavaScript? Div Element can be filtered based on class name for displaying specific content using JavaScript. Here, we will explore two different approaches to filtering a DIV element.Table of Content Using querySelectorAll and classListUsing getElementsByClassNameUsing querySelectorAll and classListIn this app 3 min read Get the Value of Text Input Field using JavaScript Getting the value of a text input field using JavaScript refers to accessing the current content entered by a user in an input element, such as <input> or <textarea>, by utilizing the value property. This allows for dynamic interaction and data manipulation in web forms.Syntax inputField 2 min read How to Get Selected Value in Dropdown List using JavaScript? A dropdown list is an HTML element that allows users to select one option from a list of options. The <select> and <option> tags are used to create dropdown lists in HTML. In this article, we will see how to get selected value from dropdown list using JavaScript.Basic HTML Structure for 2 min read How to read CSS rule values with JavaScript? DOM (Document Object Model) object can read and manipulate CSS rules. We can use the following approaches to read all the Embedded CSS rules using JavaScript. Using getElementsByTagName() MethodUsing window.getComputedStyle() MethodApproach 1: Using the getElementsByTagName() MethodUse document.getE 3 min read Like