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

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint