Javascript Reference - HTML DOM Button formEnctype Property








The formenctype attribute is new for the <button> element in HTML5.

The formenctype attribute specifies how form-data should be encoded before sending it to a server.

The formenctype attribute is only used for buttons with type="submit".

This attribute overrides the form's enctype attribute.

The formEnctype property sets or gets the value of the formenctype attribute of a button.

Browser Support

formEnctype Yes 10 Yes Yes Yes

Syntax

Return the formEnctype property:

var v = buttonObject.formEnctype 

Set the formEnctype property:

buttonObject.formEnctype='application/x-www-form-urlencoded,multipart/form-data,text/plain'




Property Values

Value Description
application/x-www-form-urlencoded All characters are encoded before sent. This is default
multipart/form-data No characters are encoded. For a file upload control
text/plain Spaces are converted to + symbols, but no special characters are encoded

Return Value

A String representing the type of content to the server.

Example

The following code shows how to get how form-data should be encoded before sending it to a server.


<!DOCTYPE html>
<html>
<body>
<!--from  w ww . j  a  va 2s.c  o  m-->
<form action="url" method="post" enctype="application/x-www-form-urlencoded">
Name: <input type="text" name="fname" value="A"><br>
<button id="myBtn" type="submit" formenctype="text/plain">Submit</button>
</form>
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myBtn").formEnctype;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the value of the formenctype attribute of a button from "text/plain" to "application/x-www-form-urlencoded".


<!DOCTYPE html>
<html>
<body>
<form action="" method="post">
Name: <input type="text" name="fname" value="A"><br>
<button id="myBtn" type="submit" formenctype="text/plain">Submit form</button>
</form><!--from  w  w w  . jav  a 2  s .com-->
<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myBtn").formEnctype = "application/x-www-form-urlencoded";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: