DEV Community

Cover image for Why You Should Separate JavaScript Access from CSS Selectors (and How to Do It Right!)
Haseeb Mirza
Haseeb Mirza

Posted on

Why You Should Separate JavaScript Access from CSS Selectors (and How to Do It Right!)

Learn why separating JavaScript logic from CSS selectors is a best practice in frontend development. Discover how data attributes can improve your code and why CSS selectors are essential for styling.
Do you use CSS selectors like classes or IDs to select HTML elements in JavaScript? If yes, then it’s time to explore a cleaner and more intuitive method for DOM selection. In this blog post, you’ll learn what Data Attributes are, why they matter, and how to keep your styling and behaviour layers separate — a habit that leads to cleaner, scalable, and maintainable code.

First Things First — What Are Data Attributes?

❓ What is an Attribute?
Attributes are extra information added to an HTML element using the name/value pair format:

<img src="./cat.jpg" />
Enter fullscreen mode Exit fullscreen mode

Here, src is a standard attribute that tells the browser where to find the image.
But what if we try something custom, like:

<img src="./cat.jpg" body-hair="fur" />
Enter fullscreen mode Exit fullscreen mode

This won’t validate — because body-hair is not a valid HTML attribute.
That’s where Data Attributes come in!
The correct version:

<img src="./cat.jpg" data-body-hair="fur" />
Enter fullscreen mode Exit fullscreen mode

Now it’s valid HTML, and we can even access it using JavaScript.

What Are Data Attributes?

Data attributes are custom attributes that:
• Start with data-
• Can be named freely (like data-user-id, data-role, data-action)
• Are accessible via JavaScript
• Don't affect styling
They’re perfect for storing information that doesn't belong to the visual style of the page — such as IDs, states, or custom logic flags.

Why You Should Separate JavaScript from CSS Selectors

Improves Code Maintainability

When you use the same CSS classes for both styling and JS access, any small change in style class names can break your functionality.
Bad Practice:

Javascript

document.querySelector('.btn-primary').addEventListener('click', handleClick);
Enter fullscreen mode Exit fullscreen mode

If

.btn-primary
Enter fullscreen mode Exit fullscreen mode

class is renamed for design reasons, your JS will break.
Better Practice:

html

<button class="btn-primary" data-action="submit">Submit</button>
document.querySelector('[data-action="submit"]').addEventListener('click', handleClick);

Enter fullscreen mode Exit fullscreen mode

Here, the JavaScript depends on a custom attribute (data-action) — not the design class.

Follows the Separation of Concerns Principle

HTML = Structure
CSS = Design
JavaScript = Behaviour
When JavaScript is tied to styling classes, these layers get mixed up. Separating them improves your project’s clarity and modularity.

Prevents Breakage During UI Changes

In large projects or teams, designers may update styles frequently. If JavaScript depends on design classes like

.red-btn or .bold-text

, every redesign risks breaking JS code.
Using data attributes or unique IDs for JavaScript access creates a stable bridge between HTML and JavaScript.

Enhances Performance and Reusability
With decoupled selectors:

• Components become reusable
• Less risk of conflicting class names
• Easier for automated testing tools to target elements

Why CSS Selectors Are Still Essential for Styling

Because:
• CSS selectors define how elements look
• They help apply colours, fonts, spacing, and layout
• Without them, your page will have no visual identity
Common CSS Selectors Used in Styling

/* class selector */
.card {
  border: 1px solid #ccc;
  padding: 20px;
}

/* ID selector */
#main-header {
  font-size: 24px;
  font-weight: bold;
}

/* element selector */
h1 {
  color: navy;
}
📱 For responsive dashboards, media queries also rely on CSS selectors:
@media (max-width: 768px) {
  .sidebar {
    display: none;
  }
}
Enter fullscreen mode Exit fullscreen mode

👉 So while JavaScript logic should be independent of class names, styling still depends heavily on them.

Best Practice Summary

Purpose Use This
Styling CSS classes, IDs
JavaScript data-* attributes, unique IDs
HTML structure Semantic tags (, , etc.)

Final Thoughts

Mixing CSS selectors and JavaScript logic may work in the short term, but it leads to fragile and messy code. By using CSS classes for styling and data attributes for JS logic, you create a clean separation of concerns, making your project easier to read, scale, and maintain.

Resources
• Using data attributes – MDN
• A Complete Guide to Data Attributes – CSS Tricks

Top comments (0)