How to change the type attribute from submit
to button
using JavaScript or jQuery? I use an input
element like <input type="submit" name="submitform"/>
.
Asked
Active
Viewed 3.5k times
7
6 Answers
24
Change the property of the native DOM node:
document.getElementsByName("submitform")[0].type = "button";
Do it with jQuery:
$("input[name='submitform']").prop("type", "button");
But remember that you cannot change input
types in Internet Explorer 8 and below.

James Allardice
- 164,175
- 21
- 332
- 312
-
.prop will not work with older jquery versions(http://stackoverflow.com/questions/5874652/prop-vs-attr) – Bogdan M. Apr 26 '13 at 07:15
-
1@BogdanM. - `.prop()` was added in jQuery 1.6. If you're still below that then it's definitely time for an upgrade. – James Allardice Apr 26 '13 at 07:16
-
Then instead of changing attribute ,shall i proceed to add onsubmit for the same form? do you have any idea @james Allardice – av1000 Apr 26 '13 at 07:45
-
@av1000 - I'm not sure what you mean. What are you actually trying to achieve? – James Allardice Apr 26 '13 at 07:51
4
You can use setAttribute property something like this in javascript
document.getElementsByName("submitform").setAttribute('type', 'button');
for jQuery
$('input[name="submitform"]').attr("type", "button");

chandresh_cool
- 11,753
- 3
- 30
- 45
-
just a heads-up: there is a difference in using .attr and .prop, see http://api.jquery.com/prop/ ("the difference between attributes and properties can be important in specific situations. Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes.") and http://stackoverflow.com/questions/5874652/prop-vs-attr – Bogdan M. Apr 26 '13 at 07:09
4
$('input').prop('type','button');

rahularyansharma
- 11,156
- 18
- 79
- 135
-
@av1000 - It will work but bear in mind the point I made in my answer - it *won't* work in IE8 and below. – James Allardice Apr 26 '13 at 07:10
-
Then instead of changing attribute ,shall i proceed to add onsubmit for the same form? do you have any idea @james Allardice – av1000 Apr 26 '13 at 07:44
0
For <input type="submit" name="submitform"/>
,
write jquery as:
<script>
$(document).ready(function(){
$('input[name="submitform"]').attr('type', 'button');
});
</script>

Zeeshan
- 493
- 2
- 11
- 25