Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery removeAttr('type') doesn't work

My problem is very simple

$('#button').removeAttr('type');

triggers an error on firebug

type property can't be changed

I have 2 questions:

  • How to get around this?
  • Is there a reference with a list of properties that can't be changed?

Thanks

EDIT

What I want to do is:

Prevent the button from submitting the form.

Note: my button is included as a html template, the form code is not accessible form me.
Something like:

include 'form.php';
include 'buttons.php';

where I have control only on buttons.php

like image 382
ilyes kooli Avatar asked Jun 14 '12 11:06

ilyes kooli


1 Answers

You can't change an input's type in IE (<9?) after it has been inserted into the DOM. jQuery raises an error for all browsers because of this.

From source:

    set: function( elem, value ) {
            // We can't allow the type property to be changed (since it causes problems in IE)
            if ( rtype.test( elem.nodeName ) && elem.parentNode ) {
                jQuery.error( "type property can't be changed" );

There are no other cases I know about (or jQuery knows about) and how to get around this depends a lot on what you are trying to do in the first place. Consider creating a new element with different type and using that for example.

Edit: To prevent the button from submitting a form, you can use:

$("#button").click(function(e){
    e.preventDefault();
});

Or (credit Tim Down):

$("#button").prop("disabled", true);

To prevent form from being submitted through any means, you can use:

$("#form").submit(function(e){
    e.preventDefault();
});
like image 93
Esailija Avatar answered Sep 25 '22 06:09

Esailija