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
  • 62,582
  • 25
  • 126
  • 161
Dreagen
  • 1,733
  • 16
  • 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
  • 258,201
  • 41
  • 486
  • 479
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
  • 428,835
  • 81
  • 738
  • 806
0

Try something like this-

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