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

javascript passing form attributes

P: n/a
Hi,

Have just found a bug in Netscape for some of my Javascript code - works ok
in Internet Explorer. Given the following form element:

<input type="text" compulsory = "yes" name="an_el">

I thought it was possible to display the value compulsory similar to getting
the value of the box, E.G .

"an_el.compulsory" instead of "an_el.value"

It seems not in Netscape / Firefox- does anyone know a way round this? - I
need to know from a different part of the code which fields are compulsory
so I can validate them.

Andy.
Dec 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK

Andy wrote:
Have just found a bug in Netscape for some of my Javascript code - works ok
in Internet Explorer. Given the following form element:

<input type="text" compulsory = "yes" name="an_el">

I thought it was possible to display the value compulsory similar to getting
the value of the box, E.G .
Not a bug but proper behavior for unknown attribute. IE does the same
in the strict mode.
It seems not in Netscape / Firefox- does anyone know a way round this? - I
need to know from a different part of the code which fields are compulsory
so I can validate them.


<style type="text/css">
..compulsory {
/*NOP*/
}
</style>
....
<input type="text" class="compulsory" name="an_el">
....
if (document.forms[i].elements[j].className == 'compulsory') { ... }

Dec 21 '05 #2

P: n/a
VK wrote:
Andy wrote:
Have just found a bug in Netscape for some of my Javascript
code - works ok in Internet Explorer. Given the following
form element:

<input type="text" compulsory = "yes" name="an_el">

I thought it was possible to display the value compulsory
similar to getting the value of the box, E.G .


Not a bug but proper behavior for unknown attribute. IE does
the same in the strict mode.


Yet another fiction from the VK collection of bogus 'facts' about web
browsers. In strict (CSS1Compat) mode IE 6 handles custom attributes in
_exactly_ the same way as it handles them in quirks (BackCompat) mode.

It would save everyone a great deal of time and trouble if you would
stop posting the products of your deranged mind to this group.

Richard.
Dec 23 '05 #3

P: n/a
Andy wrote:
Have just found a bug in Netscape for some of my Javascript
code - works ok in Internet Explorer. Given the following
form element:

<input type="text" compulsory = "yes" name="an_el">

I thought it was possible to display the value compulsory
similar to getting the value of the box, E.G .

"an_el.compulsory" instead of "an_el.value"
The handling of 'custom' attributes is outside of any formal
specification and so any associated behaviour observed in one browsers
cannot be assumed to apply to any others. Probably the majority of
browsers do not transfer 'custom' attributes to named properties of the
corresponding element in the DOM.
It seems not in Netscape / Firefox- does anyone know a
way round this? - I need to know from a different part of
the code which fields are compulsory so I can validate them.


Well, experience suggests that the - getAttribute - method of an element
may be used to retrieve the (string) value of 'custom' attributes in a
way that is widely supported and can be tested to verify that it is
viable on any given browser prior to relying upon the results. E.G:-

var val = an_el.getAttribute('compulsory');

Baring in mind that while HTML attributes are supposed not to be case
sedative it is safer to use precisely the same case in the attribute
name used with the method call as appears in the HTML mark-up (generally
it is safer to be more strict than necessary, but IE has a tendency to
be case-sensitive with get/setAttribute calls when it theoretically
should not be).

The availability of this facility may be tested/verified if you ensure
that custom attributes are always present in the context where they
would be needed. I.E. you provide both compulsory="yes" and
compulsory="no" so that any getAttribte call that returned nothing would
indicate that the facility was not available on the given client. The
testing strategy would go:-

1. Can I retrieve a reference to the element? If so do 2 -
2. Does the element implement a - getAttribute - method? If
so call it, store the result and do 3 -
3. Is the result of the getAttribute call non-null/undefined/empty
(type-converts to boolean true)? If so validate the field based
upon the retrieved value.

Any failure in the tests at steps 1, 2 or 3 indicate that no facility is
available to retrieve 'custom' attribute values on this browser and a
fall-back to server-side validation would be indicated. (server-side
validation is recommended in all cases of submitting client-side data to
a server because all client-side validation is unreliable, so no extra
work is involved in employing this fall-back.)

Richard.
Dec 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.