frm["custom"] vs frm.getAttribute("custom")?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • F. Da Costa

    #1

    frm["custom"] vs frm.getAttribute("custom")?

    Hi,

    I was wondering *why* there is a difference between the results of the
    following two statements. On the suface they seem to do the same (or do they?)

    frm["custom"] => returns void

    frm.getAttribut e("custom") => returns the value of the attribute


    The frm (= <form>) element *does* contain the custom="xyz" attribute.

    Its not a 'life or death' issue but it did surprise me a bit.

    TIA
    Fermin DCG
  • Lasse Reichstein Nielsen

    #2
    Re: frm[&quot;custom&qu ot;] vs frm.getAttribut e(&quot;custom& quot;)?

    "F. Da Costa" <dacosta@xs4all .nl> writes:
    [color=blue]
    > I was wondering *why* there is a difference between the results of the
    > following two statements. On the suface they seem to do the same (or
    > do they?)
    >
    > frm["custom"] => returns void
    > frm.getAttribut e("custom") => returns the value of the attribute[/color]
    [color=blue]
    > The frm (= <form>) element *does* contain the custom="xyz" attribute.[/color]

    In your document, maybe, but not in the HTML specification. So, you are
    not writing (correct) HTML 4.

    Notice how setting the attribute "class='foo '" in HTML creates a DOM
    property called 'className'? That means that there is not direct mapping
    between HTML attributes and DOM node properties. The properties that are
    1) defined in the HTML specification and 2) defined in the DOM specification
    will be mapped from HTML attribute to DOM property. *That* is why
    setting "id='bar'" gives a property 'id' with value 'bar', not because
    attributes and properties are necessarily the same.

    The getAttribute method gets the attribute found in the source text.
    The ["custom"] property accessor finds the property on the DOM node.
    It is quite possible to have one without the other, if the attribute/
    property name is not part of the specifications.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • -Ema-

      #3
      Re: frm[&quot;custom&qu ot;] vs frm.getAttribut e(&quot;custom& quot;)?

      F. Da Costa wrote:
      [color=blue]
      > Hi,
      >
      > I was wondering *why* there is a difference between the results of the
      > following two statements. On the suface they seem to do the same (or do
      > they?)
      >
      > frm["custom"] => returns void
      >
      > frm.getAttribut e("custom") => returns the value of the attribute
      >
      >
      > The frm (= <form>) element *does* contain the custom="xyz" attribute.
      >
      > Its not a 'life or death' issue but it did surprise me a bit.
      >
      > TIA
      > Fermin DCG[/color]
      <form name="myform" id="myform" custom="myvalue " action="..." method="POST">
      <input type="text" name="custom2" id="custom2" value="myvalue2 "/>
      </form>

      if you execute the following script on the above context:

      // ---------------------------------------------------------------------
      var frm = document.forms["myform"];
      var field = frm["custom"];
      if( field == null )
      /*1*/ alert( "Form myform don't contains a field named \"custom\"!" );
      else
      /*2*/ alert( "Field \"custom\" = " + field.value );

      var attrValue = frm.getAttribut e("custom");
      if( attrValue.lengt h = 0 )
      /*3*/ alert( "Form myform don't contains \"custom\" attribute!");
      else
      /*4*/ alert( "Attribute \"custom\" = " + attrValue );

      field = frm["custom2"];
      if( field == null )
      /*5*/ alert( "The form don't contains a field named \"custom2\"! " );
      else
      /*6*/ alert( "Field \"custom2\" = " + field.value );
      // ---------------------------------------------------------------------

      you can see alert 1, 4, 6. This is correct IMHO.
      form["fieldName"] point only to the field elements, not to form
      attributes. Only some attribute are mapped in form properties.

      Regards.
      -Ema-

      Comment

      • F. Da Costa

        #4
        Re: frm[&quot;custom&qu ot;] vs frm.getAttribut e(&quot;custom& quot;)?

        Well explained indeed.
        Thx

        Fermin

        Lasse Reichstein Nielsen wrote:[color=blue]
        > "F. Da Costa" <dacosta@xs4all .nl> writes:
        >
        >[color=green]
        >>I was wondering *why* there is a difference between the results of the
        >>following two statements. On the suface they seem to do the same (or
        >>do they?)
        >>
        >>frm["custom"] => returns void
        >>frm.getAttrib ute("custom") => returns the value of the attribute[/color]
        >
        >[color=green]
        >>The frm (= <form>) element *does* contain the custom="xyz" attribute.[/color]
        >
        >
        > In your document, maybe, but not in the HTML specification. So, you are
        > not writing (correct) HTML 4.
        >
        > Notice how setting the attribute "class='foo '" in HTML creates a DOM
        > property called 'className'? That means that there is not direct mapping
        > between HTML attributes and DOM node properties. The properties that are
        > 1) defined in the HTML specification and 2) defined in the DOM specification
        > will be mapped from HTML attribute to DOM property. *That* is why
        > setting "id='bar'" gives a property 'id' with value 'bar', not because
        > attributes and properties are necessarily the same.
        >
        > The getAttribute method gets the attribute found in the source text.
        > The ["custom"] property accessor finds the property on the DOM node.
        > It is quite possible to have one without the other, if the attribute/
        > property name is not part of the specifications.
        >
        > /L[/color]

        Comment

        Working...