If you’re only targeting modern browsers:
Use element.classList.add to add a class:
element.classList.add("my-class");
And element.classList.remove to remove a class:
element.classList.remove("my-class");
If you need to support Internet Explorer 9 or lower:
Add a space plus the name of your new class to the className
property of the element. First, put an id
on the element so you can easily get a reference.
<div id="div1" class="someclass"> <img ... id="image1" name="image1" /> </div>
Then
var d = document.getElementById("div1"); d.className += " otherclass";
Note the space before otherclass
. It’s important to include the space otherwise it compromises existing classes that come before it in the class list.
Related Posts:
- How to move all HTML element children to another parent using JavaScript?
- Google reCAPTCHA V3.0 not working. “ERROR for site owner: Invalid key type”
- What is Virtual DOM?
- How to you store data for each discord user in the discord server?
- How can I scroll to an element using jQuery?
- I am getting an “Invalid Host header” message when connecting to webpack-dev-server remotely
- How can you encode a string to Base64 in JavaScript?
- jQuery Get Selected Option From Dropdown
- How to create multidimensional array
- Open a URL in a new tab (and not a new window)
- Unexpected token u in JSON at position 0 (but only sometimes)
- Get the last item in an array
- Syntax for an async arrow function
- SyntaxError: Unexpected token o in JSON at position 1
- Syntax for an async arrow function
- (change) vs (ngModelChange) in angular
- You should not use Route or withRouter() outside a Router when using react-router 4 and styled-component in react
- React-Router only one child
- What is the most efficient way to deep clone an object in JavaScript?
- How to quickly test some javascript code?
- ReactJS: setTimeout() not working?
- Mouseover & Mouseout w/ Javascript
- Most efficient way to convert an HTMLCollection to an Array
- React Uncaught Error: Target container is not a DOM element [duplicate]
- ReactJS: setTimeout() not working?
- How to set ‘X-Frame-Options’ on iframe?
- What is the difference between node.js and io.js?
- module.exports vs exports in Node.js
- What does ‘:’ (colon) do in JavaScript?
- JavaScript getElementByID() not working
- How to send an email from JavaScript
- How can I check if an element exists in the visible DOM?
- XMLHttpRequest status 0 (responseText is empty)
- Regular expression for removing whitespaces
- How to define Global Arrays?
- JavaScript post request like a form submit
- Google Maps API DeletedApiProjectMapError
- SyntaxError: Unexpected token C in JSON at position 0 – Ionic 2 Http GET request
- Is there a simple way to make a random selection from an array in JavaScript or any other programming language?
- jsPDF fromHTML() does not show HTML
- Uncaught Invariant Violation: Rendered more hooks than during the previous render
- Get cookie by name
- jQuery equivalent of querySelector
- document.getElementById().value doesn’t set the value
- How to remove part of a string?
- How to run a function when the page is loaded?
- Convert normal date to unix timestamp
- jQuery replace one class with another
- Detect a finger swipe through JavaScript on the iPhone and Android
- console.log showing contents of array object
- How to sort 2 dimensional array by column value?
- anchor jumping by using javascript
- jQuery scrollTop() method not working
- FormData append not working
- How to convert binary string to decimal?
- Set CSS property in JavaScript?
- how to write javascript code inside php
- javascript convert int to float
- Uncaught TypeError: Cannot read property ‘toLowerCase’ of undefined
- JSLint is suddenly reporting: Use the function form of “use strict”
- How do I install Angular 2 using NPM?
- Detecting a mobile browser
- jQuery .animate() not working
- how to add inside a text box?
- Extend core block in Gutenberg
- Is it possible to enqueue the Youtube API script or does it have to be inline?
- Localhost load-scripts.php Error 500 in Dashboard [closed]
- Correctly handling WordPress core data retrieval in Gutenberg
- Javascript not included
- Gutenberg RichText
- How to use wp.hooks.addAction() in React JS/Gutenberg?
- How to run JavaScript function in WooCommerce checkout?
- Accessing customizer values in javascript
- How do display simple page for printing product info? [closed]
- Unwanted line break before
- What is the “proper” way to generate a javascript variable depending on a custom field value?
- Adding nonce or hashes to inline scripts
- WordPress Media Library: Custom tab content displayed in “Media Library” tab
- Manually mark imported photo as selected in media library
- How to retrieve data from database, then pass it to Javascript?
- Weird links found in wordpress footer
- Looping over wordpress meta to create “ ‘s?
- Install GTM in pure javascript through functions.php
- Add script to page at certain location in wordpress
- Absolutely print script in footer
- how to let users upload their custom cover image
- how to use nimble-API and Display data?
- How do you create your own link preview for my website?
- Can’t get rid of JQMIGRATE: Migrate is installed, version 3.3.2. Manually updated all the libralies and site. 5.8.2
- Redirect to homepage if attmpting to leave intranet [closed]
- Twenty seventeen theme dropdown menu issue
- How to correctly load this jquery script through the file functions.php?
- Create new product with woocommerce REST API with javascript (clientside)?
- Unserialize WP_Options options programatically?
- javascript errors on mobile browser, not on desktop
- Javascript file not included only on home page
- Get current day using javascript [closed]
- dropdown does not work [closed]
- Why is this script not working?
- Astra Mobile Cart Drawer is blank on home page