Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > "getAttribute" returns a child element instead of ...?

Reply
Thread Tools

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

 
 
Noa
Guest
Posts: n/a
 
      01-25-2006
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 ???

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      01-25-2006

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);
.....

 
Reply With Quote
 
 
 
 
BootNic
Guest
Posts: n/a
 
      01-25-2006
> "Noa" <(E-Mail Removed)> wrote:
> news:(E-Mail Removed) oups.com....
>
> 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*

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-26-2006
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
 
Reply With Quote
 
Noa
Guest
Posts: n/a
 
      01-29-2006
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

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      01-29-2006

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).

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-29-2006
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-29-2006
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-29-2006
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-30-2006
Jasen Betts wrote:

> On 2006-01-29, Thomas 'PointedEars' Lahn <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prototype: "element-id".$() instead of $('element-id') drnicwilliams Javascript 1 09-11-2006 09:51 AM
The element 'compilation' has invalid child element 'compilers'. =?Utf-8?B?Um9iZXJ0?= ASP .Net 3 11-15-2005 01:48 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM
Can TR element be direct child node of TABLE element? Patient Guy HTML 8 02-13-2005 03:25 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments