By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,778 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,778 IT Pros & Developers. It's quick & easy.

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

P: n/a
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 ???

Jan 25 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
VK

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

Jan 25 '06 #2

P: n/a
> "Noa" <nl****@jacada.com> wrote:
news:11**********************@f14g2000cwb.googlegr 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*

Jan 25 '06 #3

P: n/a
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
Jan 26 '06 #4

P: n/a
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

Jan 29 '06 #5

P: n/a
VK

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

Jan 29 '06 #6

P: n/a
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
Jan 29 '06 #7

P: n/a
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
Jan 29 '06 #8

P: n/a
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
Jan 29 '06 #9

P: n/a
On 2006-01-29, Thomas 'PointedEars' Lahn <Po*********@web.de> 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

or has someone already suggested that?
--

Bye.
Jasen
Jan 30 '06 #10

P: n/a
VK

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. Even in case if some
....strange person called his/her form "name" you still can read it (for
whatever reason) as document.forms[0]['name']. Other possible issues
are covered by my previous posts. But I guess that the issue is to
construct a less-than-probable situation and die-or-solve-it by DOMx
methods. In such case I'm not an amateur of abstract puzzles.

It is much more interesting then to OP (possibly) to challenge himself
with submit button named (again by some abstract ... strange person)
"submit" and call submit method for such form:

<input type="submit" name="submit" value="Submit">

document.forms[0].submit() == opps;

more ideas?

At least the latter happens from time to time.

Jan 30 '06 #11

P: n/a
Jasen Betts wrote:
On 2006-01-29, Thomas 'PointedEars' Lahn <Po*********@web.de> 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
Jan 30 '06 #12

P: n/a
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
Jan 30 '06 #13

P: n/a
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/
Jan 30 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.