Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I change the type of an input element to submit?

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?

like image 497
kmb64 Avatar asked Sep 10 '25 07:09

kmb64


2 Answers

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/

like image 156
adeneo Avatar answered Sep 12 '25 21:09

adeneo


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";
like image 22
J. Bruni Avatar answered Sep 12 '25 22:09

J. Bruni