Faraz
Faraz

Reputation: 6275

Set CSS for input element of type file via JavaScript

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

Answers (2)

Josh Bonnick
Josh Bonnick

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

Related Questions