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.getAttribute("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.getAttribute("custom");
if( attrValue.length = 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-