Categories
JavaScript Answers

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

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *