Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the difference between below two ways of disabling the element in java script

Tags:

javascript

I have to disable some elements in my Javas Script code , which one is better for disabling an element through Java Script :

 1.   document.getElementById("eleId").disabled = true;

 2.   document.getElementById("eleId").disabled = "disabled";

Please anyone describe these functions and what is the difference between these ?

like image 372
Peter Avatar asked Jan 23 '26 11:01

Peter


2 Answers

Both cases are valid.

In case 1, you are setting the attribute 'disabled' to the boolean value 'true'. In case 2, you are setting the attribute 'disabled' to the string value 'disabled'.

If you were going to set this value via HTML, normally you could get away with just having the bare attribute 'disabled' on the tag listed. But for XHTML/XML style compliance, you would use the well formed attribute disabled="disabled".

Basically, if the browser detects that the attribute 'disabled' is present, (I think) it will disable the element.

EDIT Another SO user (RobG) pointed out that this sets the 'DOM property' of the element in question not the 'attribute'.

When dealing with the result from 'getElementById', you are dealing with a representation of the structure of the document. When you make modifications you are settings properties on an 'object model'. Having programmed in Java for so long, I typically interchange the term property & attribute. In the case of saying "it sets the attribute", I was incorrect. Saying that I were to set the 'attribute' would mean that I would have modified the document (HTML) to change the value of "disabled" in the actual source. What this is actually doing is modifying the object tree setting the property of the representative object.

like image 157
Dave G Avatar answered Jan 27 '26 01:01

Dave G


In XHTML, attribute minimization is forbidden, and the disabled attribute must be defined as disabled="disabled"

In normal HTML, we can have like disabled=true

like image 45
Srinivas B Avatar answered Jan 27 '26 01:01

Srinivas B



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!