Reputation: 6275
I have some code to set CSS of a input type of file. I create the box dynamically using JavaScript function like this:
/*
var newLabel = document.createElement('label');
newLabel.htmlFor='${id}File';
newLabel.className='upload-file-input';
${id}_control_group.append(newLabel);
*/
var newInput = document.createElement('input');
newInput.type = 'file';
newInput.name = 'myFile';
newInput.id = 'myFile';
newInput.accept="image/png, image/jpeg, image/gif";
newInput.className='upload-file-input'; //this is not working.
form_control_group.append(newInput);
This CSS is present in class no problem about that.
Upvotes: 0
Views: 33
Reputation: 2813
newInput.classList.add('upload-file-input')
https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
The classList API is a dream.
Upvotes: 2
Reputation: 326
newInput.setAttribute("class", 'upload-file-input')
https://www.w3schools.com/jsref/met_element_setattribute.asp
When to use setAttribute vs .attribute= in JavaScript?
Upvotes: 1