AnaMaria
AnaMaria

Reputation: 3621

Disable and reset or empty all inputs

I'm trying to disable every type of input on a page and uncheck or empty the values, so I've written then following code:-

function disableAll() {
                  if ($('#chkLoginEnabled').is(':checked')) {
                      $("input").attr("disabled", false);
                  }
                  else {
                      $("input").attr("disabled", true);
                      $("input checkbox").attr('checked') = 'checked';
                      $("input textbox").val() = "";

                  }
                  $("#chkLoginEnabled").attr("disabled", false);
                }

It works to disable all the inputs (and correctly re-enables the one I need),

However, it doesn't reset any of the inputs.

Upvotes: 0

Views: 198

Answers (5)

gustavohenke
gustavohenke

Reputation: 41440

$("input:not([type='hidden'], :checkbox, :radio)").val("");
$(":checkbox, :radio").prop( "checked", false );
$(":input").prop( "disabled", true );

This will clear the value of each input that is not an checkbox, radio or hidden (which would cause you probable problems later). Those when unchecked have no "value", so we uncheck them in the second line.

Finally, in the 3rd line we disable all of them.

Upvotes: 1

mickadoo
mickadoo

Reputation: 3483

Just guesing but maybe .val() expects you to pass the parameter inside the brackets, so

$("input textbox").val() = "";

would change to

$("input textbox").val("");

Upvotes: -1

Arun P Johny
Arun P Johny

Reputation: 388406

Try

function disableAll() {
    var inputs = $('input');
    if ($('#chkLoginEnabled').is(':checked')) {
        inputs.prop('disabled', false);
    } else {
        inputs.prop('disabled', true);
        inputs.filter('[type="checkbox"]').prop('checked', false)
        inputs.filter(':text').val('')
    }
    $('#chkLoginEnabled').attr('disabled', false);
}

Upvotes: 2

Ioannis Karadimas
Ioannis Karadimas

Reputation: 7906

You need to do:

 $("input textbox").val('');

To clear a particular input.

Upvotes: -1

Adil
Adil

Reputation: 148150

Try using prop() instead of attr()

$("input").prop("disabled", false);

Upvotes: 2

Related Questions