cross-browser object/elem access

M

Matt

Here's a question I couldn't find satifactory answer on the web.
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=???

Am I being clear enough? Sorry if I am not.
 
S

Simon Wigzell

Matt said:
Here's a question I couldn't find satifactory answer on the web.
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=???

Am I being clear enough? Sorry if I am not.

You could read my post "Netscape Compatibility and the responses to it, 2
posts above yours...
 
R

Richard Cornford

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top