473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
13 3181
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
> "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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Arvid Andersson | last post by:
Hello, I need to convert a string to a number, but the string can contain +,-,* and / as well as parenthesis. For example, if I have the string "30/(6+9)" I would like a function that returned the...
2
by: CES | last post by:
All, I'm at a loss, the code below works in IE but not in Netscape and I'm unskilled enough not to know why. Essentially this code looks thru all of the form fields and if the input box has a...
3
by: F. Da Costa | last post by:
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 => returns void ...
4
by: Marc Elser | last post by:
Hi Everybody, Can someone please tell me how to access the form name if there's a form field named "name", for example: <form name="myform"> <input type="text" name="name" value="Marc">...
3
by: hutch | last post by:
Hi, I'm making use of this VERY useful function for a so called 'standards compliant' replacement for target="_blank": function externalLinks() { if (!document.getElementsByTagName) return;...
12
by: Stefano | last post by:
Hi all, what is the correct use of the "default" attribute in XML Schema? For example: <xs:element name="myProperty" type="xs:string" default="myDefaultValue"/> What can I do with it? What...
2
by: christopher.secord | last post by:
I'm having a hard time understanding an error that I'm getting. In the code below, I'm trying to call substring() on an attribute of an anchor. The error I get says "getAttribute("rel") has no...
11
by: jesdynf | last post by:
I'm having trouble applying a stylesheet to content I'm generating after the fact. Here's the sample code: <html> <head> <title>CSS/DOM Problem Example</title> <style type="text/css">...
7
by: SM | last post by:
Hello, i need to add a classname to an element <ausing JavaScript. I've tried a couple of thing with no success. This is an small piece of my code: .... <li><a href="<?php echo $cd_img; ?>"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.