December 25, 2000 - Retrieving Objects
December 25, 2000 Retrieving Objects Tips: December 2000
Yehuda Shiran, Ph.D.
|
<P ID="foo"><I>Be sure to close this paragraph.</I></P>
renders as follows:
Be sure to close this paragraph.
We can reference this tag in Internet Explorer just by its ID. For example, to figure out its location we state:
foo.style.left
This doesn't work in Netscape 6.
You cannot access objects just by their HTML ID. You need to use:
document.getElementById()
to find the object, and only then compute the object's properties. The following button calls handleClick()
upon clicking:
<SCRIPT LANGUAGE="JavaScript">
<!--
function handleClick(){
var obj = document.getElementById("button1");
alert("horizontal position = " + obj.style.left);
}
// -->
</SCRIPT>
The following works as well:
<SCRIPT LANGUAGE="JavaScript">
<!--
function handleClick(){
alert("horizontal position = " + document.getElementById("button1").style.left);
}
// -->
</SCRIPT>