Richard Korts
asked on
Javascript onFocus Blur
I want to have a normal HTML form with various input fields but in two of the fields, depending upon conditions, I don't want to allow the user to update an existing value.
As I recall, if I do the following in the field definition, it accomplishes that.
<input type = test" name="fname" value ="ABC" onFocus="blur();">
Is this correct?
Thanks
As I recall, if I do the following in the field definition, it accomplishes that.
<input type = test" name="fname" value ="ABC" onFocus="blur();">
Is this correct?
Thanks
If you decide to use jQuery, you should use the prop() method instead of attr() according to the jQuery documentation.
The .prop() method should be used to set disabled and checked instead of the .attr() method.
if (someCondition) {
$('input').prop('disabled', true);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I wouldn't recommend using the disabled property as a disabled element will not be submitted with the form.
I think the easiest route is Julian's suggested use of the readonly property although you can also make use of what Mikkel and Kim suggested and use jQuery to remove the readonly property according to the the conditions you mentioned in your original post i.e. start the form with the field being readonly and then remove the property once the qualifying conditions have been met.
I think the easiest route is Julian's suggested use of the readonly property although you can also make use of what Mikkel and Kim suggested and use jQuery to remove the readonly property according to the the conditions you mentioned in your original post i.e. start the form with the field being readonly and then remove the property once the qualifying conditions have been met.
if (someCondition) {
$('input').prop('readonly', false);
}
You can also use the "disabled" attribute in the <input> element. For example:
Open in new window
Or if you prefer jQuery:Open in new window