I've got a form with several search (text) fields. When text is entered into one of the fields, I'd like to disable the others. Conversely, if all text is removed from the active text field, the other fields should be re-enabled.
The basic idea would be something like this (I think):
$(document).ready(function() {
$("input[@type=text]").keypress(function(e) {
if(current textbox has text in it)
{
//disable all textboxes that aren't this one
}
else
{
//enable all textboxes
}
});
});
I'm not clear on what the easiest way to find/work with "all text boxes that aren't this one" is.
I think the best way to do "all textboxes that aren't this one" is using the not filter like:
$(document).ready(function() {
$(":text").keyup(function(e) {
if($(this).val() != '') {
$(":text").not(this).attr('disabled','disabled');
} else {
$(":text").removeAttr('disabled');
}
});
});
@ in the selector is not supported. :text is shorthand for what you want, if are you sticking to selecting them that way.keyup event instead of keypress.I would just disable them all first, and then since the code to be executed is triggered by an event for the control, you can use the this keyword to reference the control with focus and re-enable it.
$("input[@type=text]").attr("disabled", true);
$(this).attr("disabled", false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With