HTML DOM Input FileUpload accept Property


The HTML DOM Input FileUpload accept property returns a string, which is the value of the accept attribute of FileUpload. User can also set it to a new value.

Syntax

Following is the syntax −

  • Returning string value
inputFileUploadObject.accept
  • Setting value attribute to a string value
inputFileUploadObject.accept = value

Example

Let us see an example of Input FileUpload accept property −

 Live Demo

<!DOCTYPE html>
<html>
<head>
<title>Input FileUpload accept</title>  
<style>
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   * {
      padding: 2px;
      margin:5px;
   }
   input[type="button"]{
      border-radius: 10px;
   }
</style>
</head>
<body>
<form>
<fieldset>
<legend>FileUpload-accept</legend>
<label for="FileSelect">Upload:
<input type="file" id="FileSelect">
</label><br>
<input type="button" onclick="fileAccept('Audio')" value="Audio">
<input type="button" onclick="fileAccept('Image')" value="Image"><br>
<div id="divDisplay"></div>
</fieldset>
</form>
<script>
   var divDisplay = document.getElementById("divDisplay");
   var inputFile = document.getElementById("FileSelect");
   divDisplay.textContent = 'All Files Accepted';
   function fileAccept(fileType) {
      if(fileType === 'Audio'){
         inputFile.accept = 'audio/*';
         divDisplay.textContent = 'Only '+inputFile.accept+' Files';
      } else {
         inputFile.accept = 'image/*';
         divDisplay.textContent = 'Only '+inputFile.accept+' Files';
      }
   }
</script>
</body>
</html>

Output

This will produce the following output −

After Clicking ‘Audio’ button −

After clicking ‘Image’ button −

Updated on: 30-Jul-2019

67 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements