JavaScript set the label text | Simple example code

Use the innerHTML property to change or set the text inside the label in JavaScript. The innerHTML property sets or returns the HTML content of an element.

document.getElementById('label').InnerHTML = 'your text goes here'; 

JavaScript set label text Example

You can change or set the text of a label using JavaScript. Here is the HTML example code on click button label text will be changed by the function.

<!DOCTYPE html> 
<html> 

<body> 

	<label id = "lb"> Welcome </label> 
	
	<br> 
	
	<button onclick="changeTxt()"> Click Here </button> 

	<script> 
		function changeTxt() { 
			document.getElementById('lb').innerHTML = 'Text changed'; 
		} 
	</script> 
</body> 

</html>	 

Output:

JavaScript set the label text example

Do comment if you have any doubts and suggestion on this basic Examples of JS.

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.