All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Get Label Value In JavaScript

Last Updated : Mar 11, 2024

How To Get Label Value In JavaScript

In this tutorial we will show you the solution of how to get label value in JavaScript, here we collecting label value by using getElementById() method as we know this is widely help us in script we can retrieve all html form elements values too easily.

The getElementById() method returns an Element object that represents an HTML element with an id that matches a specified string.

This is one of the most common methods in the HTML DOM.

Step By Step Guide On How To Get Label Value In JavaScript :-

Here we defined label with id attribute ‘lbl’ and it had some text.

In script we defined variable ‘r’ for storing label value and we can display that label value using ‘innerText or textContent’ properties in console.log() method.

The innerText property used to get or set the text inside an HTML element and its descendants and also textContent property had same behavior it returns the content of an element/node and all of its descendents.

<!DOCTYPE html>
        <title>GET LABEL TEXT</title>
        <label id="lbl">Welcome message from label</label>
           var r=document.getElementById('lbl');
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined label tag with id ‘lbl’ and within that we specified some text of ‘Welcome message from label’.
  7. In script we defined variable ‘r’ for storing label ‘lbl’ value and then collected label value displayed using ‘innerText, textContent’ properties on console panel.
  8. For seeing result we need to open console panel on browser so we have right click on browser webpage and select ‘inspect’ option then console panel will opened successfully.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to get label value using javascript.

When we executes program on browser we can see the label text ‘Welcome message from label’ on webpage. For collect and displaying label value we need to use javascript properties as seen above.

Then for open console panel we had another option use shortcut ‘ctrl+shift+j’ on browser and at right hand side console panel will open with retrieved label value and it will displayed two time because we used two properties for showing result.

I hope this tutorial on how to get label value in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪