DOM question

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

    DOM question

    I have the following line of code:

    var newopt = document.create Element("option ");

    I addChild this to a select list node and it works fine.

    I want to attach an object reference to newopt, but the system doesn't
    allow me to do this:
    newopt.createAt tribute("graph" )

    nor does it allow this:
    newopt.setAttri bute("graph",ne w MyObject)

    I can't find any mention in the DOM that attributes cannot be added to
    element nodes.

    My question, then, is: how can I attach an object reference to an option
    node?

  • oeyvind toft

    #2
    Re: DOM question

    Dont know for sure this is what you were looking for but..

    This work in IE6:

    <html>
    <head>
    <title></title>
    <script type="text/javascript">
    function init(){

    var newOption = new Option('string' , 'optionValue');
    var elSelect = document.getEle mentById("selec tID");
    var elDiv = document.getEle mentById("divID ");

    elSelect.option s[0] = newOption;
    var attr = document.create Attribute('grap h');

    elSelect.option s[0].setAttributeNo de(attr);
    elSelect.option s[0].setAttribute(' graph', elDiv);
    alert(elSelect. options[0].getAttribute(' graph').tagName );
    }
    </script>
    </head>

    <body onload="init()" >
    <div id="divID"></div>
    <select id="selectID"> </select>
    </body>
    </html>

    Oeyvind
    --





    Comment

    • Michael Winter

      #3
      Re: DOM question

      On Fri, 10 Sep 2004 08:58:32 +0200, oeyvind toft <oeyvtoft@onlin e.no>
      wrote:
      [color=blue]
      > Dont know for sure this is what you were looking for but..
      >
      > This work in IE6:[/color]

      However, it won't work in any other browser. The setAttribute method takes
      two arguments; both strings. All that will happen on compliant browsers is
      the object will have its toString method called, and the result will be
      assigned to the attribute.

      [snip]

      Mike

      --
      Michael Winter
      Replace ".invalid" with ".uk" to reply by e-mail.

      Comment

      • Michael Winter

        #4
        Re: DOM question

        On Fri, 10 Sep 2004 01:24:58 GMT, Richard Trahan <rtrahan@optonl ine.net>
        wrote:

        [snip]
        [color=blue]
        > newopt.setAttri bute("graph",ne w MyObject)
        >
        > I can't find any mention in the DOM that attributes cannot be added to
        > element nodes.
        >
        > My question, then, is: how can I attach an object reference to an option
        > node?[/color]

        As I said in response to Oeyvind's message, the setAttribute approach
        cannot work as the arguments must be strings. However, you don't need to
        worry about the *Attribute methods as you can create a new property just
        as you would with any other object:

        newopt.graph = new MyObject();

        The *Attribute methods are designed to dynamically create attributes that
        you would normally write directly with HTML. As you're not doing this,
        it's inappropriate to use them. That said, HTML attributes (but not
        XHTML/XML attributes) can be directly accessed using their property names,
        like aObj.href and imgObj.alt.

        Hope that helps,
        Mike

        --
        Michael Winter
        Replace ".invalid" with ".uk" to reply by e-mail.

        Comment

        • Richard Trahan

          #5
          Re: DOM question

          Michael Winter wrote:[color=blue]
          > newopt.graph = new MyObject();
          >[/color]

          Thank you for responding, Mike. That does work; I seem to remember
          trying that first and having trouble with it, but it works fine now.

          However, this doesn't work:

          var clone = child.cloneNode (true)

          'child' is a select list option node, and contains my graph property
          which I set with
          child.graph = new MyObject()

          'graph' disappears after the cloneNode(). I'm using Venkman on nn 7.1
          and graph is clearly present in child and absent in clone. The DOM
          reference on cloneNode indicates that everything should be copied,
          including subtrees (which isn't relevant here). Could it be that the
          browser interpets "clone" to refer only to values, not to attributes,
          and will therefore only yield attributes defined in the DOM? If so, I
          would have to maintain a parallel set of structures, which is a PITA.
          I'm new at all this, so I may be missing something.

          Comment

          • Michael Winter

            #6
            Re: DOM question

            On Fri, 10 Sep 2004 16:51:57 GMT, Richard Trahan <rtrahan@optonl ine.net>
            wrote:

            [snip]
            [color=blue]
            > var clone = child.cloneNode (true)
            >
            > 'child' is a select list option node, and contains my graph property
            > which I set with
            > child.graph = new MyObject()
            >
            > 'graph' disappears after the cloneNode(). I'm using Venkman on nn 7.1
            > and graph is clearly present in child and absent in clone. The DOM
            > reference on cloneNode indicates that everything should be copied,
            > including subtrees (which isn't relevant here). Could it be that the
            > browser interpets "clone" to refer only to values, not to attributes,
            > and will therefore only yield attributes defined in the DOM?[/color]

            Clone copies all attributes, even custom ones, but you aren't adding an
            attribute (you can't, as I explained). There is no requirement that
            cloneNode copies user-defined properties.
            [color=blue]
            > If so, I would have to maintain a parallel set of structures, which is a
            > PITA.[/color]

            Why? You could just write something like:

            function cloneGraph() {
            var t = this.cloneNode( true);
            if(t) {t.graph = this.graph;}
            return t;
            }

            function createOption() {
            var e = document.create Element('OPTION ');
            if(e) {
            e.graph = new MyObject();
            e.clone = cloneGraph;
            }
            return e;
            }

            var child = createOption();
            // do stuff
            var clone = child.clone();

            unless I'm missing something you're not telling us.

            Hope that helps,
            Mike

            --
            Michael Winter
            Replace ".invalid" with ".uk" to reply by e-mail.

            Comment

            • Richard Trahan

              #7
              Re: DOM question

              Michael Winter wrote:
              (snip)

              Thanks again. I was confused about the meaning of clone, due partly to
              inconsistent wording in the DOM: "The duplicate node returned by
              cloneNode() has no parent. Cloning a node copies all of its attributes
              and their values..."

              To me, "duplicate" means everything, including user properties, but
              "attributes and their values" apparently implies "and nothing else".

              Learning this stuff is a nightmare. Can you recommend a book or tutorial
              on the DOM?

              Comment

              • Michael Winter

                #8
                Re: DOM question

                On Fri, 10 Sep 2004 19:38:35 GMT, Richard Trahan <rtrahan@optonl ine.net>
                wrote:
                [color=blue]
                > Michael Winter wrote:
                > (snip)
                >
                > Thanks again. I was confused about the meaning of clone, due partly to
                > inconsistent wording in the DOM: "The duplicate node returned by
                > cloneNode() has no parent. Cloning a node copies all of its attributes
                > and their values..."
                >
                > To me, "duplicate" means everything, including user properties, but
                > "attributes and their values" apparently implies "and nothing else".[/color]

                I don't think it's inconsistent; the apparent deviation is a product of
                the nature of Javascript. In other languages that provide bindings for the
                DOM (Java and C++, for example), you aren't able to modify an object. They
                must be defined in advance.

                In Java, you'd create a class that implements the Element interface (or
                perhaps inherits from a class that already does), add your graph member,
                and modify the cloneNode method to copy the graph and call the base class'
                cloneNode. This is much more explicit, though it is, in fact, exactly what
                I suggested.
                [color=blue]
                > Learning this stuff is a nightmare. Can you recommend a book or tutorial
                > on the DOM?[/color]

                Not personally. I learn through specifications and experimentation . :)

                I can only suggest you check the resources in the FAQ
                (<URL:http://jibbering.com/faq/>), though there don't seem to be many
                appropriate links. Others reading this thread might be able to suggest
                more.

                Good luck,
                Mike

                --
                Michael Winter
                Replace ".invalid" with ".uk" to reply by e-mail.

                Comment

                • Richard Cornford

                  #9
                  Re: DOM question

                  Richard Trahan wrote:
                  <snip>[color=blue]
                  > Thanks again. I was confused about the meaning of clone,
                  > due partly to inconsistent wording in the DOM: "The
                  > duplicate node returned by cloneNode() has no parent.
                  > Cloning a node copies all of its attributes and their
                  > values..."
                  >
                  > To me, "duplicate" means everything, including user
                  > properties, but "attributes and their values" apparently
                  > implies "and nothing else".[/color]
                  <snip>

                  You should bear in mind that the DOM specifications are intended to be
                  language neutral. Adding arbitrary properties to objects is a very
                  ECMAScritp thing to be doing (and as DOM objects would qualify as 'host
                  objects' there is actually no good reason to expect that to work, beyond
                  precedence), in a language such as Java adding a property to an object
                  in a DOM implementation is unthinkable. That means that the
                  specification cannot imply behaviour that cannot be implemented in any
                  language (only the bindings for specific languages could do that).

                  Richard.


                  Comment

                  Working...