How to get a custom button’s text value with JavaScript?

Sometimes, we want to get a custom button’s text value with JavaScript.

In this article, we’ll look at how to get a custom button’s text value with JavaScript.

How to get a custom button’s text value with JavaScript?

To get a custom button’s text value with JavaScript, we can use the textContent or innerText property.

For instance, we write:

<button>
  hello
</button>

to add a button.

Then we write:

const elem = document.querySelector('button');
const txt = elem.textContent || elem.innerText;
console.log(txt)

to select the button with document.querySelector.

And then we get the text content of the button with textContent or innerText.

Therefore, txt is 'hello'.

Conclusion

To get a custom button’s text value with JavaScript, we can use the textContent or innerText property.