Link to home
Start Free TrialLog in
Avatar of Richard Korts
Richard KortsFlag for United States of America

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
Avatar of Mikkel Sandberg
Mikkel Sandberg
Flag of United States of America image

Hey Richard,
You can also use the "disabled" attribute in the <input> element. For example:
if (someCondition) {
  document.getElementsByTagName("input").disabled = true;
}

Open in new window

Or if you prefer jQuery:
if (someCondition) {
  $('input').attr('disabled', true);
}

Open in new window

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);
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
if (someCondition) {
    $('input').prop('readonly', false);
}

Open in new window