How to find the accept-charset and enctype attribute of a form in JavaScript?


In this article we are going to discuss how to find the accept-charset and enctype attribute of a form in JavaScript.

In HTML, there is a <form> element which has few attributes − input, label, text area, select, name, target. The accept-charset is used to return the set of one or more encoding types for the HTML document. This attribute also specifies the character encodings that are to be used for the form submission. The enctype is used to specify how the form-data is encoded.

Using acceptCharset attribute

The acceptCharset attribute in JavaScript will display the acceptable charset values.

Syntax

The following is the syntax of acceptCharset attribute in JavaScript −

document.getElementById(‘formID’).acceptCharset;

Accept-charset in a form. The common values for accept-charset are utf-8(Unicode encoding) and ISO-8859-1(Latin alphabet encoding).

Example

The following is an example program to display the acceptable character set i.e., acceptCharset of the form.

<!DOCTYPE html>
<html>
<head>
   <title>Find the accept-charset and enctype attribute of a form in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <div id="form-div" style="text-align : center">
      <form id="myForm" name="Login Form" action="/action_page.php" target="_self" accept-charset="utf-8">
         First name: <input type="text" name="fname" ><br>
         Last name: <input type="text" name="lname" ><br>
         <input type="submit" value="Submit">
      </form>
      <p id="form"></p>
   </div>
   <script>
      document.getElementById('form').innerHTML = 'accept-charset of the form : '+document.getElementById('myForm').acceptCharset;
  </script>
</body>
</html>

On executing the above code, the following output is generated.

Using enctype attribute

The enctype attribute of JavaScript will display the enctype of the form.

Syntax

The following is the syntax of the enctype attribute in JavaScript −

document.getElementById(‘formID’).enctype;

If the value of method attribute is post. The possible values for enctype are as follows.

  • application/x-www-form-urlencoded − This is the default type of enctype.

  • multipart/form-data − This value used when the user needs to upload a file.

  • text/plain − This value is used for debugging purposes. It sends data without encoding.

Example

The below program is an example to display the enctype of the form.

<!DOCTYPE html>
<html>
<head>
   <title>Find the accept-charset and enctype attribute of a form in JavaScript</title>
   <meta charset="UTF-8">
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
   <div id="form-div" style="text-align : center">
      <form id="myForm" name="Login Form" action="/action_page.php" target="_self" enctype="multipart/form-data">
         First name: <input type="text" name="fname" ><br>
         Last name: <input type="text" name="lname" ><br>
         Profile picture : <input type="file" id="profile" name="profile" accept="image/jpeg"><br>
         <input type="submit" value="Submit">
      </form>
      <p id="form"></p>
   </div>
   <script>
      document.getElementById('form').innerHTML = 'enctype of the form : '+document.getElementById('myForm').enctype;
   </script>
</body>
</html>

On executing the above code, the following output is generated.

Updated on: 08-Dec-2022

237 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements