If the element has no class, give it one. Otherwise, append a space followed by the new className:
var el = document.getElementById('hello');
if(el) {
el.className += el.className ? ' someClass' : 'someClass';
}
More Related Contents:
- How to work out values in javascript
- Javascript – is it possible to make a loop using functions? [closed]
- How to change data php to javascript json [closed]
- “Idiot Check” in JavaScript (.toLowerCase and .toUpperCase) [duplicate]
- How to check first 200 chekboxes , then next 200 and so on?
- How can I group an array of objects by key?
- How can I access the contents of an iframe with JavaScript/jQuery?
- Change the URL in the browser without loading the new page using JavaScript
- JavaScript global event mechanism
- Resize image with javascript canvas (smoothly)
- JavaScript variable definition: Commas vs. Semicolons
- How to transform black into any given color using only CSS filters
- No-Javascript Detection Script + Redirect
- Is JSON Hijacking still an issue in modern browsers?
- Finding Variable Type in JavaScript
- Setting vendor-prefixed CSS using javascript
- How to set the focus for a particular field in a Bootstrap modal, once it appears
- How to handle floats and decimal separators with html5 input type number
- How to extract base URL from a string in JavaScript?
- How do i disable a submit button when checkbox is uncheck?
- console.log(result) prints [object Object]. How do I get result.name? [duplicate]
- Hide 401 console.error in chrome dev tools getting 401 on fetch() call [duplicate]
- Scroll into view in react
- Running a command in a Grunt Task
- How to click a browser button with JavaScript automatically?
- Getting setting cookies on different domains, with JavaScript or other
- JavaScript EventSource SSE not firing in browser
- How do I interpolate a variable as a key in a JavaScript object?
- How is Google Meet able to show CPU usage?
- How could I change window’s location without reloading and # hack?