1

I have an input field which is disabled or not based on its contents. However I also have a button which changes the value of this field using the javascript getElementById('field_name').value = "something".

Is it possible to have it so that this will not change the value of the field if it is disabled?
I have tried both setting the field to readonly and disabled, but this doesn't stop the button from changing its value

scunliffe
  • 60,441
  • 24
  • 124
  • 159
Dreagen
  • 1,731
  • 17
  • 21

3 Answers3

1

The simplest way is probably to perform the check in your button's click handler.

Instead of doing:

document.getElementById("field_name").value = "something";

Do:

var element = document.getElementById("field_name");
if (!element.disabled) {
    element.value = "something";
}
Frédéric Hamidi
  • 249,845
  • 40
  • 466
  • 467
0

If you want to have the setting code to respect the disabled attribute, simply make it so:

var el = getElementById('field_name');
if (!el.disabled) el.value = "something";
Jon
  • 413,451
  • 75
  • 717
  • 787
0

Try something like this-

if(!getElementById(field_name).disabled){
          getElementById(field_name).value = "something";
 }
sgowd
  • 2,162
  • 21
  • 29