cross-browser object/elem access

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

    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.w hatever=???

    Am I being clear enough? Sorry if I am not.
  • Simon Wigzell

    #2
    Re: cross-browser object/elem access


    "Matt" <mathfield@hotm ail.com> wrote in message
    news:c7et0p$47e $1@news1.ucsd.e du...[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.w hatever=???
    >
    > 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...


    Comment

    • Richard Cornford

      #3
      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.w hatever=???[/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 = getElementWithI d("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.whatev er = "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.


      Comment

      Working...