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

N

Noa

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 ???
 
V

VK

Noa said:
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);
.....
 
B

BootNic

Noa said:
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">?
 
T

Thomas 'PointedEars' Lahn

Noa said:
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
 
N

Noa

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
 
V

VK

Noa said:
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).
 
T

Thomas 'PointedEars' Lahn

Noa said:
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>
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.
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
 
T

Thomas 'PointedEars' Lahn

VK said:
Noa said:
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
 
T

Thomas 'PointedEars' Lahn

T

Thomas 'PointedEars' Lahn

Jasen said:
VK said:
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
 
T

Thomas 'PointedEars' Lahn

VK said:
Jasen said:
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
 
R

Randy Webb

Thomas 'PointedEars' Lahn said the following on 1/30/2006 8:25 AM:
VK said:
Jasen said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top