470,618 Members | 1,786 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,618 developers. It's quick & easy.

cross-browser object/elem access

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.
Jul 23 '05 #1
2 1210

"Matt" <ma*******@hotmail.com> wrote in message
news:c7**********@news1.ucsd.edu...
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...
Jul 23 '05 #2
Matt wrote:
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:-

<URL: http://jibbering.com/faq/faq_notes/a...ite.html#getEl >
<URL: http://jibbering.com/faq/faq_notes/n...ct.html#bdGEID 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.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by ampo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.