---
title: "Discussion on \"Separate your Javascript Access from CSS Selectors\" | Hashnode"
description: "Discussion on \"Separate your Javascript Access from CSS Selectors\". Do you use CSS selectors like classes or id's to select the HTML elements on the Document Object Model? If yes, then get ready to learn an more intuitive way to select the DOM elements because in this article we will be learning about how and why to ..."
url: "https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Separate your Javascript Access from CSS Selectors](https://ayushcodes.hashnode.dev/separate-your-javascript-access-from-css-selectors?utm_source=hashnode&utm_medium=feed)

Do you use CSS selectors like classes or id's to select the HTML elements on the Document Object Model? If yes, then get ready to learn an more intuitive way to select the DOM elements because in this article we will be learning about how and why to ...

Feb 5, 2021 · 7 min read · ayushcodes.hashnode.dev  
Read the article

-   [#javascript](https://hashnode.com/tag/javascript)
-   [#css3](https://hashnode.com/tag/css3)
-   [#web-development](https://hashnode.com/tag/web-development)
-   [#frontend-development](https://hashnode.com/tag/frontend-development)
-   [#css](https://hashnode.com/tag/css)

## Written by

[Ayush Saini](https://hashnode.com/@AyushCodes)

@AyushCodes · earth

  
[View profile](https://hashnode.com/@AyushCodes)

software engineer

professional div renderer

## Responses 9

Markdown supported · ⌘↵ to post

[Catalin Pit](https://hashnode.com/@Catalinpit)

[Feb 9, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/602230157d860b3782e8ee05)

Great article!

P.S: I also love the article image!

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 9, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/602230157d860b3782e8ee05#reply-60223db77d860b3782e8ef79)

Thanks, Catalin 😊 for reading it. I made the image myself 💡

[Gaurav Tewari](https://hashnode.com/@tewarig)

[Feb 8, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/6021206e7d860b3782e8d579)

Thanks for sharing this.. it would help me a lot.

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 9, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/6021206e7d860b3782e8d579#reply-6021e5ef281265375d643722)

Glad to help

[Badal sharma](https://hashnode.com/@badal897)

[Feb 6, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601e26eb1054fa16dadd86c8)

Great blog

[Victoria Lo](https://hashnode.com/@victoria)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d6bb51054fa16dadd7a8e)

Thanks for highlighting the importance of readability in code! Great article too :)

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d6bb51054fa16dadd7a8e#reply-601d701f072bc516ff6f40d6)

Thank you, Victoria 🙏. I appreciate it.

[Victor Neves](https://hashnode.com/@vneves)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d0ad51054fa16dadd71f1)

I use a different approach with a js prefix class. Attributes should be used to pass data and not for targeting element even so it's better than targeting an ID or a CSS class.

e.g:

```
<button class="js-percentage-button btn-red">%</button>
```

```
const percentageButton = document.querySelector('.js-percentage-button')
```

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d0ad51054fa16dadd71f1#reply-601d6f6c1054fa16dadd7b47)

Indeed Victor, I too love to use that approach but what I realized is that selecting elements with data attributes can greatly improve the code readability and maintenance. Thanks for reading it 💖

[Favourite Jome](https://hashnode.com/@Favourite)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d01311054fa16dadd712a)

Thanks for sharing, I recently ran into an issue where I had to change my class name in both my HTMl and JS file. Now I'll use the `data-` attribute instead.

Also I like the way you write 😊, easily to understand 👍

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d01311054fa16dadd712a#reply-601d6cbf1054fa16dadd7ac5)

Thanks, Jome for your kind words 🙏

[Syarif](https://hashnode.com/@sy4rif)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d0104072bc516ff6f366f)

Very interesting topic, But don't you think that just by adding another class name only for JavaScript selector, give you the same approach?, thanks.

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601d0104072bc516ff6f366f#reply-601d6e071054fa16dadd7b1f)

Yes Syarif, you're absolutely correct but IMO a class should be used for styling elements, but again these are just personal preferences. Thanks for reading it 💖

[Akash Bhandwalkar](https://hashnode.com/@akash19)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601ce739072bc516ff6f33e5)

Wow, amazing content and representations as well. I was feeling like you're talking when you used "not convinced Ayush"

[Ayush Saini](https://hashnode.com/@AyushCodes)

[Feb 5, 2021](https://hashnode.com/posts/separate-your-javascript-access-from-css-selectors/601cce7d1054fa16dadd6db3/comment/601ce739072bc516ff6f33e5#reply-601d6bfe1054fa16dadd7aaa)

Thank you, Akash 💗
