Both of these return true. Is one boolean where the other is text. Shouldnt the one with no .value error as it is refering to the object and not the objects value attribute? Please help me understand this a bit more.
<input type="text" name="sigstatus1" id="sigstatus1" style="display:none;" size="40" fieldlength="50"></input>
<input type="text" name="sigstatus2" id="sigstatus2" style="display:none;" size="40" fieldlength="50"></input>
var sigStatus1 = document.getElementById("sigstatus1").value;
var sigStatus2 = document.getElementById("sigstatus2");
sigStatus1 = true;
sigStatus2 = true;
alert("Sig1 " + sigStatus1 + "\nSig2 " + sigStatus2);
fiddle http://jsfiddle.net/Xp7jX/
The direct answer to your question is that:
alert("Sig1 " + sigStatus1 + "\nSig2 " + sigStatus2);
displays both values as true because right before that line of code, you set both values to true:
sigStatus1 = true;
sigStatus2 = true;
alert("Sig1 " + sigStatus1 + "\nSig2 " + sigStatus2);
It looks like you think that this code:
var sigStatus1 = document.getElementById("sigstatus1").value;
sigStatus1 = true;
will set the DOM element value to true. It will not.
When you do:
var sigStatus1 = document.getElementById("sigstatus1").value;
you are simply getting the value out of the DOM element and putting it in a local variable. Since it's a text field, that value will be a text string.
When you then do:
sigStatus1 = true;
you are just replacing the contents of that local variable with the Boolean value true
(it doesn't do anything to the DOM element). When you then display that variable in an alert, javascript converts the Boolean value to text (for display purposes) and you see "true".
If you wanted to set the display the value from the DOM element, you would just do this:
var sigStatus1 = document.getElementById("sigstatus1").value;
alert(sigStatus1);
If you wanted to set the value of the DOM element, you would do this:
document.getElementById("sigstatus1").value = "whatever";
Both values are boolean true
, you are overwriting the values explicitly:
sigStatus1 = true;
sigStatus2 = true;
Maybe you were looking for this?
var sigStatus1 = document.getElementById("sigstatus1").value;
var sigStatus2 = document.getElementById("sigstatus2");
alert("Sig1 " + sigStatus1 + "\nSig2 " + sigStatus2);
Variable sigStatus2
should alert something like [object HTMLDivElement]
(which is generated internally by the objects toString
method).
true
manually (sigStatus1 = true;
andsigStatus2 = true;
), so why are you surprised that they have the valuetrue
afterwards - Niklas B. 2012-04-04 19:40