Suppose I have the following html DOM element:
<input type="checkbox" id="theckboxid" checked="true" />
I know that in Javascript I can access to the checked
property (or any other property of the element) by using a bunch of ways. I'll focus on the following two ways:
var isChkd = document.getElementById('theckboxid').checked;
or just
var isChkd = theckboxid.checked;
In both cases the result will be that into the variable isChkd
will be stored the Boolean value true
.
Even using the consolle in Chrome I get exactly the same result.
So the questions are:
Why is better to use getElementById
instead the direct use of the id to access the element and its properties?
What are pro and cons of both approaches?