spacer
Yehuda Shiran December 25, 2000
Retrieving Objects
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

The worst habit to overcome when scripting Netscape 6 is how to reference HTML objects. Internet Explorer has turned us into spoiled scripters. The following HTML tag, for example:

<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>


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business