Connecting Tech Pros Worldwide Forums | Help | Site Map

cross-browser object/elem access

Matt
Guest
 
Posts: n/a
#1: Jul 23 '05
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.

Simon Wigzell
Guest
 
Posts: n/a
#2: Jul 23 '05

re: cross-browser object/elem access



"Matt" <mathfield@hotmail.com> wrote in message
news:c7et0p$47e$1@news1.ucsd.edu...[color=blue]
> 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.[/color]

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


Richard Cornford
Guest
 
Posts: n/a
#3: Jul 23 '05

re: cross-browser object/elem access


Matt wrote:[color=blue]
> Here's a question I couldn't find satifactory answer on the web.[/color]

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[color=blue]
>[/color]
[color=blue]
> 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=???[/color]

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.


Closed Thread