"getAttribute" returns a child element instead of ...?

Discussion in 'Javascript' started by Noa, Jan 25, 2006.

  1. Noa

    Noa Guest

    Hi

    I have a page that looks like that:

    <form name="kuku1" action ="anotherpage.html" >
    <input name="name">
    <input name="kuku2">
    </form>

    As far as i know, "getAttribute" should return a string value of an
    elements' attribute.

    document.forms[0].getAttribute("action") indeed returns the string
    "anotherpage.html"
    document.forms[0].action will also return that string

    However, document.forms[0].getAttribute("name") does not return the
    string "kuku1", but it returns the first input object.
    In a similar way , document.forms[0].getAttribute("kuku2") returns the
    second input element, and not a null object (or an empty string) as i
    would expect.

    Is it a bug? is it a defined behavoiur?
    And how can i get the name of the form ???
    Noa, Jan 25, 2006
    #1
    1. Advertising

  2. Noa

    VK Guest

    Noa wrote:
    > Hi
    >
    > I have a page that looks like that:
    >
    > <form name="kuku1" action ="anotherpage.html" >
    > <input name="name">
    > <input name="kuku2">
    > </form>
    >
    > As far as i know, "getAttribute" should return a string value of an
    > elements' attribute.
    >
    > document.forms[0].getAttribute("action") indeed returns the string
    > "anotherpage.html"
    > document.forms[0].action will also return that string
    >
    > However, document.forms[0].getAttribute("name") does not return the
    > string "kuku1", but it returns the first input object.
    > In a similar way , document.forms[0].getAttribute("kuku2") returns the
    > second input element, and not a null object (or an empty string) as i
    > would expect.
    >
    > Is it a bug?


    No, just wrong usage of right tools. Drop it for now.

    > And how can i get the name of the form ???


    alert(document.forms[0].name);
    alert(document.forms[0].action);
    alert(document.forms[0].method);
    alert(document.forms[0].enctype);

    alert(document.forms[0].elements[0].name);
    alert(document.forms[0].elements[1].name);
    .....
    VK, Jan 25, 2006
    #2
    1. Advertising

  3. Noa

    BootNic Guest

    > "Noa" <> wrote:
    > news:....
    >
    > Hi
    >
    > I have a page that looks like that:
    >
    > <form name="kuku1" action ="anotherpage.html" >
    > <input name="name">
    > <input name="kuku2">
    > </form>
    >
    > As far as i know, "getAttribute" should return a string value of an
    > elements' attribute.
    >
    > document.forms[0].getAttribute("action") indeed returns the string
    > "anotherpage.html"
    > document.forms[0].action will also return that string
    >
    > However, document.forms[0].getAttribute("name") does not return the
    > string "kuku1", but it returns the first input object.
    > In a similar way , document.forms[0].getAttribute("kuku2") returns
    > the second input element, and not a null object (or an empty
    > string) as i would expect.
    >
    > Is it a bug? is it a defined behavoiur?
    > And how can i get the name of the form ???


    What would happen if it change <input name="name"> to <input name="_name">?

    --
    BootNic Wednesday, January 25, 2006 10:18 AM

    Every time I close the door on reality it comes in through the windows.
    *Jennifer Unlimited*
    BootNic, Jan 25, 2006
    #3
  4. Noa wrote:

    > I have a page that looks like that:
    >
    > <form name="kuku1" action ="anotherpage.html" >
    > <input name="name">
    > <input name="kuku2">
    > </form>
    >
    > As far as i know, "getAttribute" should return a string value of an
    > elements' attribute.
    >
    > document.forms[0].getAttribute("action") indeed returns the string
    > "anotherpage.html"
    > document.forms[0].action will also return that string
    >
    > However, document.forms[0].getAttribute("name") does not return the
    > string "kuku1", but it returns the first input object.


    To be exact, a reference to the first (HTML)Input(Element) object.

    > In a similar way , document.forms[0].getAttribute("kuku2") returns the
    > second input element, and not a null object (or an empty string) as i
    > would expect.


    The `null' value and the empty string are very different things. You should
    not expect that a value of `object' type like `null' is returned where a
    string is specified, as it is here.

    > Is it a bug?


    Yes, indeed you have discovered another bug in an Element::getAttribute()
    implementation, and you provided another reason why it should not be used
    in the HTML DOM and why direct property accesses should be used instead,
    until further notice.

    > is it a defined behavoiur?


    No, it is not. Element::getAttribute() is designed to return the value
    of the attribute of the element that is represented by the respective
    Element object in the DOM. That would the the HTMLFormElement object
    representing the `form' element here.

    > And how can i get the name of the form ???


    Do not name any form control "name", to be exact never use an identifier
    of an attribute of `form' elements or a property of (HTML)Form(Element)
    objects as name or ID of a form control (if it is child of a `form'
    element). However, it is unlikely that you need the name of the form or
    need to name the form in the first place; try to use the `this' reference
    instead.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 26, 2006
    #4
  5. Noa

    Noa Guest

    PointedEars, Thank you for the prompt answer

    > Yes, indeed you have discovered another bug in an Element::getAttribute()
    > implementation, and you provided another reason why it should not be used
    > in the HTML DOM and why direct property accesses should be used instead,
    > until further notice.


    Is this what you meant? -> document.forms[0].name
    Because this does the exactly same thing as "getAttribute".... it
    returns the html input element and not the string with the value of the
    attribute....

    > Do not name any form control "name", to be exact


    It is not "my" page so i cannot change it... I write BHO that reads
    info from a page that could be any page on the net... and unfortunately
    there is a possibility that i need to look for a specific form in a
    page according to its name. So , isn't there any other way?

    And, an off-topic question - How do I report the bug ?...

    Thanks again, Noa
    Noa, Jan 29, 2006
    #5
  6. Noa

    VK Guest

    Noa wrote:
    > Is this what you meant? -> document.forms[0].name


    See my post (""VK") again please:

    document.forms[0].elements[0].name
    or
    document.forms[0].elements['name'].name

    > Because this does the exactly same thing as "getAttribute".... it
    > returns the html input element and not the string with the value of the
    > attribute....


    Yes, and to complete the fun, you can add to your form
    <input type="submit" name="submit" value="submit">

    and now try document.forms[0].submit();

    Conclusion:
    1) do not name elements using property names as names. Are we short on
    vocabulary? ;-)
    2) do not try to explain form and form elements using DOM1 or DOM2.
    This is a pre-historic construction kept for legacy requirements. Use
    *form methods* for forms.

    > And, an off-topic question - How do I report the bug ?


    It is not a bug and I doubt very much that it will be ever fixed in the
    way you want. But if it makes you feel better, you may complain to:

    http://bugzilla.mozilla.org
    http://support.microsoft.com/ph/2073
    https://bugs.opera.com/wizard/

    (ask for more when you are done with above).
    VK, Jan 29, 2006
    #6
  7. Noa wrote:

    > PointedEars, Thank you for the prompt answer


    You're welcome. However, I would have appreciated it if you provided
    attribution of quoted material.

    <URL:http://jibbering.com/faq/faq_notes/pots1.html#ps1Post>
    <URL:http://www.safalra.com/special/googlegroupsreply/>

    >> Yes, indeed you have discovered another bug in an Element::getAttribute()
    >> implementation, and you provided another reason why it should not be used
    >> in the HTML DOM and why direct property accesses should be used instead,
    >> until further notice.

    >
    > Is this what you meant? -> document.forms[0].name


    It is.

    > Because this does the exactly same thing as "getAttribute"....
    > it returns the html input element and not the string with the value of
    > the attribute....


    Only because "name" is used as name of a control of that form.

    >> Do not name any form control "name", to be exact

    >
    > It is not "my" page so i cannot change it...


    It was a general recommendation. Please mark omissions in quoted material.

    > I write BHO that reads info from a page that could be any page on the
    > net... and unfortunately there is a possibility that i need to look for
    > a specific form in a page according to its name. So , isn't there any
    > other way?


    You could try formRef.getAttributeNode("name").value or
    formRef.getAttributeNodeNS("", "name").value, or use pattern
    matching. The first two, specified in DOM Level 2 Core, work
    in Firefox 1.5 as well.

    > And, an off-topic question - How do I report the bug ?...


    That depends on which user agents you have tested with.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 29, 2006
    #7
  8. VK wrote:

    > Noa wrote:
    >> Is this what you meant? -> document.forms[0].name

    >
    > See my post (""VK") again please:
    >
    > document.forms[0].elements[0].name
    > or
    > document.forms[0].elements['name'].name


    The OP is trying to retrieve the `form' element's name, specified by
    its `name' attribute.

    >> And, an off-topic question - How do I report the bug ?

    >
    > It is not a bug [...]


    Certainly it is. Element::getAttribute() should return the _attribute
    value_ for all elements, not a reference to a child form control. Do
    you ever read before you post?


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 29, 2006
    #8
  9. VK wrote:

    > 2) do not try to explain form and form elements using DOM1 or DOM2.
    > This is a pre-historic construction kept for legacy requirements. Use
    > *form methods* for forms.


    Nonsense.

    <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-882764350>
    (As I said, the usage deprecated there is usually the more reliable one.)

    <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064>
    <URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-40002357>


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 29, 2006
    #9
  10. Jasen Betts wrote:

    > On 2006-01-29, Thomas 'PointedEars' Lahn <> wrote:
    >> VK wrote:
    >>> Noa wrote:
    >>>> Is this what you meant? -> document.forms[0].name
    >>> See my post (""VK") again please:
    >>>
    >>> document.forms[0].elements[0].name
    >>> or
    >>> document.forms[0].elements['name'].name

    >> The OP is trying to retrieve the `form' element's name, specified by
    >> its `name' attribute.

    > hmm...
    >
    > document.forms[0].attributes['name'].value


    Nice one.

    > or has someone already suggested that?


    Not explicitly. formRef.attributes['name'] refers to the same object
    formRef.getAttributeNode('name') or formRef.getAttributeNodeNS('', 'name')
    refer to per _Gecko DOM_, whereas I suggested the last two already.
    However, the former is worth another try.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 30, 2006
    #10
  11. VK wrote:

    > Jasen Betts wrote:
    >> hmm...
    >> document.forms[0].attributes['name'].value
    >> or has someone already suggested that?

    >
    > It was suggested to use form methods for form. [...]


    There is no such thing as a "form method". Let us just
    leave the rest of your misconceptions to /dev/null again.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 30, 2006
    #11
  12. Noa

    Randy Webb Guest

    Thomas 'PointedEars' Lahn said the following on 1/30/2006 8:25 AM:
    > VK wrote:
    >
    >> Jasen Betts wrote:
    >>> hmm...
    >>> document.forms[0].attributes['name'].value
    >>> or has someone already suggested that?

    >> It was suggested to use form methods for form. [...]

    >
    > There is no such thing as a "form method".


    You missed the boat on this one Thomas. He didn't say use the "form
    method", he said "use form methods for form" meaning, you use methods
    related to the form to manipulate forms.

    One "method" of accessing a form:

    document.form['formID']

    Another "method" to access it:

    document.getElementById('formID')

    Neither of which is a "method of the form" but it is a "form method" to
    access the form.

    --
    Randy
    comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
    Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
    Randy Webb, Jan 30, 2006
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Um9iZXJ0?=
    Replies:
    3
    Views:
    6,820
    Steven Cheng[MSFT]
    Nov 15, 2005
  2. Patient Guy
    Replies:
    8
    Views:
    747
    Roy Schestowitz
    Feb 13, 2005
  3. Jeff Rodriguez
    Replies:
    23
    Views:
    1,093
    David Schwartz
    Dec 9, 2003
  4. Trans
    Replies:
    2
    Views:
    214
    Trans
    Nov 6, 2005
  5. drnicwilliams
    Replies:
    1
    Views:
    77
    Richard Cornford
    Sep 11, 2006
Loading...

Share This Page