JavaScript get label text | Simple example code

Use the innerHTML property to get the text value of the label in JavaScript. The innerHTML property sets or returns the HTML content of an element.

var value = document.getElementById('idName').textContent

If you need to target < IE9 then you need to use .innerText

JavaScript get label text Example

HTML example code:

You can get value of any label or any other element is selected by its attribute id:

<!DOCTYPE html> 
<html> 
<body> 

	<label id = "lb"> Fetch me 10000 </label> 
	
	<br> 
	
	<button onclick="getTxt()"> Click Here </button> 

	<script> 
		function getTxt() { 
			var value = document.getElementById('lb').innerHTML;
			alert(value);
		} 
	</script> 
</body> 

</html>	 

Output:

JavaScript get label text example code

Do comment if you have any doubts on this basic JS topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.