25

I'm calling this function:

function submit_button(button_id){
    $('#' + button_id).attr('type', 'submit');
}

to make this button type = submit instead of button.:

<input  name="confirm_button" id="confirm_button" type="button" value="Confirm" class="login_btn" />

and I get this error (in firefox):

uncaught exception: type property can't be changed

Are there any work arounds?

PeeHaa
  • 66,697
  • 53
  • 182
  • 254
kmb64
  • 1,483
  • 2
  • 17
  • 28

7 Answers7

45
function submit_button(button_id){
    $('#' + button_id).prop('type', 'submit');
}

Be aware that this changes the type but not the value, so the button will still read "confirm".

A fiddle to prove it : http://jsfiddle.net/qeUxP/

adeneo
  • 293,187
  • 26
  • 361
  • 361
14

Why jQuery won't allow you to just change the type attribute?

Because it causes problems in Internet Explorer.

More info at: change type of input field with jQuery

"Straight from the jQuery source":

// We can't allow the type property to be changed (since it causes problems in IE)
if ( name == "type" && jQuery.nodeName( elem, "input" ) && elem.parentNode )
    throw "type property can't be changed";
Community
  • 1
  • 1
J. Bruni
  • 19,262
  • 11
  • 70
  • 90
5

It worked finally , try finding the following:

   $("#confirm_button").each(function () 
   { this.type = "number"; });
JOBG
  • 4,364
  • 4
  • 23
  • 47
Basem Sayej
  • 3,284
  • 1
  • 12
  • 12
4

Here's a workaround:

$('<input name="confirm_button" id="confirm_button2" type="submit" value="Confirm" class="login_btn" />').insertAfter('#confirm_button');
$('#confirm_button').remove();
Alexis Wilke
  • 15,168
  • 8
  • 60
  • 116
Candide
  • 28,318
  • 6
  • 50
  • 57
3

Why not just bind to the click event and call the form submit in your click handler?

davidethell
  • 10,959
  • 5
  • 37
  • 59
2

javascript:

function submit_button(){

      var btn=document.getElementById('button_id');
      btn.setAttribute('type', 'submit');

  }

======================================================================

Jquery:

function submit_button(){

 $('#' + button_id).prop('type', 'submit');

   }

======================================================================

moffeltje
  • 4,095
  • 4
  • 24
  • 49
saidesh kilaru
  • 644
  • 2
  • 10
  • 18
0

The workaround is to create an element of the correct type, and replace the currently exisitng one.

However, in your example, a <button> element could serve the purpose of submitting the form, render as a button, and not require any javascript to do so, eg:

<button name="confirm_button" id="confirm_button" type="input" value="Confirm" class="login_btn" disabled />

And then to make the button enabled again:

$('confirm_button').removeAttr('disabled');
Rich O'Kelly
  • 38,811
  • 8
  • 78
  • 108
  • This button is hidden up until the user clicks another button. Then this button is shown so the user can click it to submit the form. The problem Im having is that this submit button is still triggered by pressing the enter key even when it is still hidden @davidethell – kmb64 Dec 04 '11 at 21:12
  • @davidethell, you could just add the `disabled` attribute to your hidden button. Answer updated. – Rich O'Kelly Dec 04 '11 at 21:34