10 examples of 'javascript create element with class' in JavaScript

Every line of 'javascript create element with class' 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
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}
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}
7function makeElement(document, type, className, parent) {
8 const el = document.createElement(type);
9 if (className) {
10 el.className = className;
11 }
12 if (parent) {
13 parent.appendChild(el);
14 }
15 return el;
16}
59export function create(tagName, className, container) {
60 var el = document.createElement(tagName);
61 el.className = className || '';
62
63 if (container) {
64 container.appendChild(el);
65 }
66 return el;
67}
131function div_class(cls,contents) { return wrap_class("div",cls,contents); }
144function create(tag, className) {
145 var el = document.createElement(tag)
146 if (className) el.className = className
147 return el
148}
171function createDiv(cls) {
172 var el = document.createElement('div');
173 el.className = cls || '';
174 return el;
175}
1function createEl(elemType, id, css) {
2 // create the element
3 var elem = document.createElement(elemType);
4
5 // set the id and css text
6 elem.id = id;
7 elem.style.cssText = css || '';
8
9 // return the new element
10 return elem;
11} // createEl
51function createElement (tagName, className) {
52 const elment = document.createElement(tagName)
53 elment.className = className
54 return elment
55}
71function createElement(tagName, className) {
72 const elment = document.createElement(tagName)
73 elment.className = className
74 return elment
75}

Related snippets