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.
formEnctype |
Yes | 10 | Yes | Yes | Yes |
Return the formEnctype property:
var v = buttonObject.formEnctype
Set the formEnctype property:
buttonObject.formEnctype='application/x-www-form-urlencoded,multipart/form-data,text/plain'
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 |
A String representing the type of content to the server.
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:
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: