0

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.

Paul
  • 23,002
  • 11
  • 77
  • 112
AnaMaria
  • 3,130
  • 3
  • 16
  • 36

5 Answers5

2

Try using prop() instead of attr()

$("input").prop("disabled", false);
Adil
  • 139,325
  • 23
  • 196
  • 197
2

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);
}
Arun P Johny
  • 365,836
  • 60
  • 503
  • 504
1
$("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.

gustavohenke
  • 38,209
  • 13
  • 113
  • 120
-1

You need to do:

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

To clear a particular input.

Ioannis Karadimas
  • 7,296
  • 3
  • 32
  • 45
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – Deepu Jul 18 '13 at 12:53
  • @Deepu: You are incorrect. The author asks why the fields are not cleared, and the answer is quite valid. Take the time to read the question the next time you decide to criticize an answer. – Ioannis Karadimas Jul 18 '13 at 13:46
-1

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

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

would change to

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

mickadoo
  • 2,969
  • 1
  • 18
  • 33