0

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.

Faraz
  • 6,025
  • 5
  • 31
  • 88

2 Answers2

2
newInput.classList.add('upload-file-input')

https://developer.mozilla.org/en-US/docs/Web/API/Element/classList

The classList API is a dream.

Josh Bonnick
  • 2,281
  • 1
  • 10
  • 21
1
newInput.setAttribute("class", 'upload-file-input')

https://www.w3schools.com/jsref/met_element_setattribute.asp

When to use setAttribute vs .attribute= in JavaScript?

David
  • 306
  • 4
  • 13