Reputation: 3621
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
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
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
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
Reputation: 7906
You need to do:
$("input textbox").val('');
To clear a particular input.
Upvotes: -1
Reputation: 148150
Try using prop()
instead of attr()
$("input").prop("disabled", false);
Upvotes: 2