It should work, in a way, on certain conditions.
It may not work by different reasons. For example,
document.getElementById
can return null because the element may not be found; "btnValidate7" should be the value of the attribute
id
of wanted element, moreover, all
id
values should be unique in the context of the whole page. One invalid
id
value can make the whole page incorrect. In this case, here is what happens next:
v.disabled
tries to refererence
v
, which is null, which in turn throws an exception which you simply don't see because you are not handling exceptions. In the browsers, unhandled JavaScript exceptions are blocked on top; they are left unnoticed.
In other cases, it may happen that the found element is not an input element; with such elements, you cannot use
disabled
. Also, I cannot see where this function is called; I don't know if it is called at all or on what stage of execution it is called. What if the element is not yet created or not rendered?
Why, after all, you put this
debugger
statement? If you need to debug code, actually debug it. You will see all values of all object on each step.
Here is a complete minimal HTML with script for you to compare; it does work:
<html>
<head>
<title>Test disabled</title>
</head>
<body>
<button id="btnValidate7">Test</button>
<script>
function validatePanel() {
var v = document.getElementById('btnValidate7');
v.disabled = true;
}
validatePanel();
</script>
</body>
</html>
This is not the biggest problem you have. Much bigger problem is how you do programming (and ask questions) in general. For example, look at your "
if
". Probably, you wanted to disable an element if it is not disabled. But why checking for it? If the element is not disabled, it will be disabled, if it is already disabled, it will remain disabled. So, why would you need this check. Besides, the check is not really correct. First, you need to make sure
v
is not null or
undefined
, by the reasons I explained in first paragraph, but let's say you already debugged the code and made sure it is a valid input element. You already know that
v.disabled
is true of false. But then it would be enough to check if
(!v.disabled)
. I'm just using your example, in fact, the whole check in pointless. Besides, you need to know what if returns for different operands, and learn about real meaning of ==, !==, === and !== operators:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Comparison_operators[
^].
Let's see… Your first question if of July 2014. Even if it was time you started to learn programming, this time would be enough to learn programming basics. Yes, you are still a beginner, but not to the extent where this fact can be used for excuses. :-)
—SA