Disabling HTML Input Elements

If you’re using IE, you can disable all input elements inside a div by setting “disabled=true”.  Unfortunately (thanks to standards compliance nonsense 😉 ) it doesn’t work anywhere else.

If you’re using jQuery, take a look here.  If not, this bit of script might help:

function SetDisabledStates( container, newStates )
{
    var states = [];

    var replace = function( element )
    {
        states[ element.id ] = element.disabled;
        element.disabled = isArray( newStates ) ? newStates[ element.id ] : newStates;
    };

    var inputs = container.getElementsByTagName("input");
    for ( var i = 0; i < inputs.length; ++ i ) replace( inputs[ i ] );

    var selects = container.getElementsByTagName( "select" );
    for ( var i = 0; i < selects.length; ++ i ) replace( selects[ i ] );

    return states;
}

function isArray( obj )
{
   return obj.constructor.toString().indexOf("Array") != -1;
}

To preserve and override the states, do something like this:

var disabledStates = SetDisabledStates( myContainer, true );

 

Then to restore the states, do this:

SetDisabledStates( myContainer, disabledStates );

 

4 Comments

  1. thanx to ppl like you who think that w3c is [quote]Unfortunately (thanks to standards compliance nonsense )[/quote] our (web dev’s) work gets 1000x harder. when these dinosaurs (as with ie) dissappear, there will be no dev’s hell anymore :/

    Reply

  2. sorry, i was hot that day 🙂
    u know, all this non-compliances between browsers – make the world go round.
    anyways, imho ie is a dinosaur that must disappear =D one thing that ie has, is DXImageTransform that no browser has yet, even on Win.

    Reply

Leave a comment