How do I select my CSS module class using document.querySelector?
You need to select it as a class,
document.querySelector(`.${styles.scrollValue}`)
Calling styles.property
returns a value such as the following,
scrollValue: '_src_styles_module__scrollValue'
So when you call document.querySelector it's looking for _src_styles_module__scrollValue
which has no selector type such .
, or #
which makes it look for an element that would match <_src_styles_module__scrollValue>
(in this case). Since there is no element with that name or the hashed name it will return null.
Working demo
How to grab a DOM element in Next.js when class name is randomized
The solution is to refer to the class in the querySelector like so:
const nav = document.querySelector(`.${styles.nav}`);
This assumes that styles were imported like so:
import styles from '../styles/Nav.module.sass';
This is based on the accepted response to this question:
How do I select my CSS module class using document.querySelector?
Is there a way to get all classes using document.querySelectorAll in reactjs and manipulate it?
I know of useRef() but in my case I am trying to select all buttons throughout the entire application. Not just one element in the current component.
Using .querySelector
or any other selector will select only those elements that are currently rendered, actually. So if you e.g. toggle the state and component re-renders with different elements, they will not be affected with your change, which will result in partially toggled theme for different elements.
You should either set a context, wrapping whole App or set a redux variable holding info which theme is currently selected. Then, you will be able to manipulate styles using e.g. theme
in styled components: https://styled-components.com/docs/advanced#theming or just toggling classNames with css modules, basing on that variable.
React CSS module not working on certain selectors
I'd say you should use className={styles[item.className] || ''}
.
item.className
will not refer to CSS module.
document.querySelector() is not applying style
Please see the following working snippet. Also keep in mind that AngularJS documentation discourages using controllers to manipulate the DOM.
angular.module("app", [])
.controller("demo", function($scope) {
$scope.p = document.querySelector('.bgColor');
Object.assign($scope.p.style, {
'background-color': 'red'
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<p ng-app="app" ng-controller="demo" class="bgColor">
This is dummy content
</p>
Related Topics
Path-Relative Style Sheet Import Vulnerabilities
How to Adjust Bootstrap's Container Div to 100Px Off The Left Viewport Edge
Vh/% Units and Keyboard on Mobile Devices
How to Reuse React-Native Stylesheet (Styles) in React
Matching First Element in Whole Document
iPhone Sticky Menu Jquery Onscroll iOS 9
Select Every Second Element of a Series of Nested Elements (Each Has Only One Child)
Using Rems with a Pixel Fallback
Unexpected Empty Space Using Flexbox
Why Does Input's Size Increase in CSS Grid Layouts
How to Automatically Add Browser Prefix to CSS3 Properties
How to Add Multiple Browser Specific Values into a CSS Style in React
CSS Background to Stretch to Window Bottom
Background Image Width Not 100% on Ipad
Ie9 + CSS: Problem with Fixed Header Table
Font-Weight:900 Only Working in Firefox
CSS Positioning Absolute Within Table Cells Not Working in Firefox