Chris Cavanagh’s Blog

Disabling HTML Input Elements

May 28, 2009 · 4 Comments

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 );

 

Categories: .NET

4 responses so far ↓

  • luka // July 10, 2009 at 7:54 am | Reply

    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 :/

  • luka // July 10, 2009 at 7:55 am | Reply

    anyway, jquery alt is quite useful :) +r

  • Chris Cavanagh // July 10, 2009 at 8:21 am | Reply

    Ouch luka, very ouch :( Actually I’m a big fan of web standards – sorry I wasn’t clearer! :)

  • luka ramishvili // July 13, 2009 at 1:43 am | Reply

    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.

Leave a Comment