DEV Community

Gustavo Henrique Silva Tenório
Gustavo Henrique Silva Tenório

Posted on

1

Tip to get selectors to use in javascript

Guys, this is a simple tip to get the selector very easily. The browser inspection copy function.

Well, sometimes that functionality is very useful if you are handling a lot of HTML injections from these new javascript frameworks. Is expected that you do not need this, but someday, maybe this helps you save a little time.

Just inspect the code, use the right-click in your tag element and select the copy option on the menu, and then choose copy selector or copy js path. (Same as the image example)

image

Ex:
document.querySelector("#root > div > div.rightPanel > div.presentMe > div.name")

NOTE.1: Most of the time you should reduce the size of the selector. (Depends on the situation, analyze pls)

Ex:
document.querySelector("rightPanel div.name")

That is it :D! You got the selector.
Thanks, Vinicius Tartari for sharing this knowledge with me :)

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️