Matt said:
Here's a question I couldn't find satifactory answer on the web.
There is a tradition with technical Usenet groups that prior to posting
to a group it is necessary to read the group's FAQ, which directly
references several implementations of scripts intended to address this
problem, including:-
I want to access element in the html (such as <div>) by the
id tag. I want to do it in browser-independent way.
how can I write a function that's basically like this:
function obj(id)...
and I can use it like this?
obj(id).style.whatever=???
You could but it would be a very bad idea to attempt to script like that
on a web browser. Very simply there is no method guaranteed to return a
reference to an IDed element on all browsers. Browsers are just not that
consistent. As element retrieval method will return null or undefined
when no element is avalable with the given ID, it is usual to implement
cross-browser element retreaval functions so that they return null
whenever the browser will not provide the required reference. As a
result it is normal to employ such a method in a more cautions way,
testing the returned value to ensure that it is not null or undefined
prior to doing anything with the reference:-
var el = getElementWithId("elId");
if(el && el.style){ //apply type-converting test to the returned
// value. Objects type-convert to boolean true,
// while null and undefined type-convert to boolean
// false.
el.style.whatever = "something"; //the el.style object exists so a
// value can be assigned to a
// property of it.
/* However, that does not mean that the implementation will be
interested
in this assignment, or react to it. But at worst this would be the
harmless creation of an expando property. Additional testing would be
required to determine if this action was meaningful to the browser.
*/
}
Richard.