10 examples of 'javascript add class to div' in JavaScript

Every line of 'javascript add class to div' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
198function addClassToDiv(div, className) {
199 const arr = div.className.split(" ");
200 if (arr.indexOf(className) === -1) {
201 div.className += ` ${className}`;
202 }
203}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

154function addClass(element_id, classname) {
155 document.getElementById(element_id).className += (" " + classname);
156}
2296function addClassName(element, className) {
2297 if (!(element = $(element))) return;
2298
2299 if (!hasClassName(element, className))
2300 element.className += (element.className ? ' ' : '') + className;
2301
2302 return element;
2303}
131function div_class(cls,contents) { return wrap_class("div",cls,contents); }
104function setClassName(id, className) {
105 var ele = document.getElementById(id);
106 if (ele) {
107 ele.className = className;
108 }
109}
20function addDiv(propName, propVal) {
21 var div = document.createElement('div');
22 div.style.marginTop = '20px';
23 div[propName] = propVal;
24 sidebar.append(div);
25}
171function createDiv(cls) {
172 var el = document.createElement('div');
173 el.className = cls || '';
174 return el;
175}
45function addClass(className) {
46 const firstChild = this.getFirstChild();
47
48 firstChild.addClass(className);
49}
18function addClass(className) {
19 this.classList.add(className);
20
21 return this;
22}
144function appendDIV(event) {
145 var div = document.createElement('div');
146 div.innerHTML = event.data || event;
147 chatContainer.insertBefore(div, chatContainer.firstChild);
148 div.tabIndex = 0;
149 div.focus();
150
151 document.getElementById('input-text-chat').focus();
152}

Related snippets