Get Button text JavaScript

In this tutorial we will learn How to Get Button text with JavaScript. innerHTML property, innerText property and textContent property can be used along with getElementById() method to select button tag and to get it's text.

innerHTML property

innerHTML property sets or returns the HTML content of HTML tag including it's text and the tags inside it.

In this example innerHTML property is used to get the text of button tag.

innerText property

innerText property sets or returns only text content of HTML tag including the text of all it's child elements.

We can also use innerText property to get text of button.

textContent property

textContent property sets or returns text content of HTML tag including the text of it's child elements which are used for text formatting.

textContent property is also used to get text of button element.

getElementById() method

getElementById() method is used to select HTML element by it's id.

In this example getElementById() method is used to select button tag.

HTML Code

HTML Code is given below, in this code we have a HTML button with onclick event.

<!DOCTYPE html>
<html>
<head>
<title>JavaScript Get Button text</title>
</head>             
<body>

<button onclick="getButtonText()" id='btn' >Click Me!</button>    
    
<script src="script.js"></script>    
</body>
</html>

JavaScript Code

Take a look at the JavaScript code, In this code user defined function getButtonText() will get the text of button.

Using this code you can also get the text of another button on the same page by using id of that button.

<script>
function getButtonText()
{
    var button = document.getElementById('btn');
    var text = button.innerHTML;
    alert(text);
}
</script>

Using innerText property.

<script>
function getButtonText()
{
    var button = document.getElementById('btn');
    var text = button.innerText;
    alert(text);
}
</script>

Using textContent property.

<script>
function getButtonText()
{
    var button = document.getElementById('btn');
    var text = button.textContent;
    alert(text);
}
</script>

Demo

Video Tutorial

Watch video tutorial on how to Get button text using JavaScript.

How To Change Image Size with JavaScript How to change Image opacity with JavaScript How to Change image src on click with JavaScript How to get the lang attribute value in JavaScript How to Get attribute Value using JavaScript How To Check if Attribute Exists or not with JavaScript How To Count number of links on Page with JavaScript How To Scroll Page to Bottom using JavaScript How To Detect Shift Key Press with JavaScript Change Text Color On Hover with JavaScript Hide and Show div using JavaScript Get Button text with JavaScript Get textarea value with JavaScript Get table row Height with JavaScript Auto Increase width of input field with JavaScript Set Textarea maxLength with JavaScript Set Textarea Value with JavaScript JavaScript Count list items JavaScript set input field value Count Button Clicks with JavaScript Scroll Page to the Top on Click with JavaScript Change id of Element with JavaScript JavaScript Change li Text Color JavaScript Change li Text JavaScript Remove id from Element