7

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"/>.

skuntsel
  • 11,624
  • 11
  • 44
  • 67
av1000
  • 135
  • 1
  • 2
  • 9

6 Answers6

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
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
1

Using jQuery:

$('input[name="submitform"]').attr('type', 'button');
Bogdan M.
  • 1,128
  • 1
  • 9
  • 23
0

with jquery:

$('input[name="submitform"]').attr('type','button')
Anh Tú
  • 636
  • 7
  • 21
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