473,626 Members | 3,325 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

frm["custom"] vs frm.getAttribut e("custom")?

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["custom"] => returns void

frm.getAttribut e("custom") => returns the value of the attribute
The frm (= <form>) element *does* contain the custom="xyz" attribute.

Its not a 'life or death' issue but it did surprise me a bit.

TIA
Fermin DCG
Jul 20 '05 #1
3 1819
"F. Da Costa" <da*****@xs4all .nl> writes:
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["custom"] => returns void
frm.getAttribut e("custom") => returns the value of the attribute The frm (= <form>) element *does* contain the custom="xyz" attribute.


In your document, maybe, but not in the HTML specification. So, you are
not writing (correct) HTML 4.

Notice how setting the attribute "class='foo '" in HTML creates a DOM
property called 'className'? That means that there is not direct mapping
between HTML attributes and DOM node properties. The properties that are
1) defined in the HTML specification and 2) defined in the DOM specification
will be mapped from HTML attribute to DOM property. *That* is why
setting "id='bar'" gives a property 'id' with value 'bar', not because
attributes and properties are necessarily the same.

The getAttribute method gets the attribute found in the source text.
The ["custom"] property accessor finds the property on the DOM node.
It is quite possible to have one without the other, if the attribute/
property name is not part of the specifications.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
F. Da Costa wrote:
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["custom"] => returns void

frm.getAttribut e("custom") => returns the value of the attribute
The frm (= <form>) element *does* contain the custom="xyz" attribute.

Its not a 'life or death' issue but it did surprise me a bit.

TIA
Fermin DCG

<form name="myform" id="myform" custom="myvalue " action="..." method="POST">
<input type="text" name="custom2" id="custom2" value="myvalue2 "/>
</form>

if you execute the following script on the above context:

// ---------------------------------------------------------------------
var frm = document.forms["myform"];
var field = frm["custom"];
if( field == null )
/*1*/ alert( "Form myform don't contains a field named \"custom\"!" );
else
/*2*/ alert( "Field \"custom\" = " + field.value );

var attrValue = frm.getAttribut e("custom");
if( attrValue.lengt h = 0 )
/*3*/ alert( "Form myform don't contains \"custom\" attribute!");
else
/*4*/ alert( "Attribute \"custom\" = " + attrValue );

field = frm["custom2"];
if( field == null )
/*5*/ alert( "The form don't contains a field named \"custom2\"! " );
else
/*6*/ alert( "Field \"custom2\" = " + field.value );
// ---------------------------------------------------------------------

you can see alert 1, 4, 6. This is correct IMHO.
form["fieldName"] point only to the field elements, not to form
attributes. Only some attribute are mapped in form properties.

Regards.
-Ema-

Jul 20 '05 #3
Well explained indeed.
Thx

Fermin

Lasse Reichstein Nielsen wrote:
"F. Da Costa" <da*****@xs4all .nl> writes:

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["custom"] => returns void
frm.getAttrib ute("custom") => returns the value of the attribute


The frm (= <form>) element *does* contain the custom="xyz" attribute.

In your document, maybe, but not in the HTML specification. So, you are
not writing (correct) HTML 4.

Notice how setting the attribute "class='foo '" in HTML creates a DOM
property called 'className'? That means that there is not direct mapping
between HTML attributes and DOM node properties. The properties that are
1) defined in the HTML specification and 2) defined in the DOM specification
will be mapped from HTML attribute to DOM property. *That* is why
setting "id='bar'" gives a property 'id' with value 'bar', not because
attributes and properties are necessarily the same.

The getAttribute method gets the attribute found in the source text.
The ["custom"] property accessor finds the property on the DOM node.
It is quite possible to have one without the other, if the attribute/
property name is not part of the specifications.

/L

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
9540
by: amywolfie | last post by:
I would like to put code behind a Find button on a form which: 1) Performs a find based on a field on the form 2) If NO RECORDS ARE FOUND, then displays a custom "No Records Found" message box. Thanks!! amy ===
6
4864
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of the html page controls the form fields that are required. It doesn't function like it's supposed to and I can leave all the fields blank and it still submits the form. Also I can't get it to transfer the file in the upload section. The file name...
0
8705
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8638
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8365
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5574
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4092
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.