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.

1
  • What is working? Commented Oct 20, 2020 at 19:36

2 Answers 2

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

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

The classList API is a dream.

Sign up to request clarification or add additional context in comments.

Comments

1
newInput.setAttribute("class", 'upload-file-input')

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

When to use setAttribute vs .attribute= in JavaScript?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.