Categories
JavaScript Answers

How to select all div text with single mouse click with JavaScript?

Spread the love

Sometimes, we want to select all div text with single mouse click with JavaScript.

In this article, we’ll look at how to select all div text with single mouse click with JavaScript.

How to select all div text with single mouse click with JavaScript?

To select all div text with single mouse click with JavaScript, we can use the window.getSelection and selectAllChildren methods.

For instance, we write

window.getSelection().selectAllChildren(document.getElementById(id));

to call getElementById to get an element with the given id.

Then we call getSelection to return an object with the selectAllChildren method to get all child nodes from the element with the given id.

Conclusion

To select all div text with single mouse click with JavaScript, we can use the window.getSelection and selectAllChildren methods.

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 *