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

passing meta-knowledge about a form element

P: n/a
I need to create a table where each element has certain specific properties.

For example, elements in the first column should always have a range of
0-100, and increment by 1. Elements in column 2 might have a range of
0-360, and increment by 0.1. Altogether there might be 8 elements in
each row.

Since this is basically the same javascript function over and over, I
wonder if there is a way to create some <input> element that might
contain this knowledge in a way that javascript can access it.

For example, could I have

<input type="text" class="percent" onkeypress="javascript:blah(event)">

where the javascript function could retrieve the class value?

Since this is going to be fairly complex, I'd like to make it fairly
self-documenting and simple. It's much easier for a maintainer to
understand class="precent" than "javascript:blah(0,100,1,event)",
especially since I am also going to have to pass navigation information
(is this a border element in the table? what are my neighbors? kind
of information... - so that the actual information might end up being
javascript:blah(0,100,1,5,21,16,18,0,1,0,0,event) to denote the data
type, the 4 neighbors, and the 4 border conditions....
Jan 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
VK

Captain Dondo wrote:
I need to create a table where each element has certain specific properties.

For example, elements in the first column should always have a range of
0-100, and increment by 1. Elements in column 2 might have a range of
0-360, and increment by 0.1. Altogether there might be 8 elements in
each row.

Since this is basically the same javascript function over and over, I
wonder if there is a way to create some <input> element that might
contain this knowledge in a way that javascript can access it.

For example, could I have

<input type="text" class="percent" onkeypress="javascript:blah(event)">

where the javascript function could retrieve the class value?


Yes, you can. Though DOM doesn't provide a native method like
getElementsByClassName, but you can write one or use any of already
written of this kind. Also if it's given what you are working only with
inputs within one form, then the task even more simple:

<input type="text" class="percent" onkeypress="blah(event)">
(please note that event handler is not a link, so javascript:
pseudo-protocol is out of business here)
....
var elm = document.form['myForm'].elements;
var len = elm.length;
for (var i=0; i<len; i++) {
switch (elm[i].className) {
case 'percent' :
// your action
break;
case 'degree':
// your action
break;
default:
// no match
}
}
....
you can apply several classes to one element to make the system as
complicated as you want:
<input type="text" class="percent mandatory " onkeypress="blah(event)">
<input type="text" class="integer positive mandatory"
onkeypress="blah(event)">
<input type="text" class="integer positive optional"
onkeypress="blah(event)">
etc.

Jan 20 '06 #2

P: n/a
VK wrote:
Yes, you can. Though DOM doesn't provide a native method like
getElementsByClassName, but you can write one or use any of already
written of this kind. Also if it's given what you are working only with
inputs within one form, then the task even more simple:

<input type="text" class="percent" onkeypress="blah(event)">
(please note that event handler is not a link, so javascript:
pseudo-protocol is out of business here)
OK, noted. I am a javascript newbie....
...
var elm = document.form['myForm'].elements;
var len = elm.length;
for (var i=0; i<len; i++) {
switch (elm[i].className) {
case 'percent' :
// your action
break;
case 'degree':
// your action
break;
default:
// no match
}
}
...
you can apply several classes to one element to make the system as
complicated as you want:
<input type="text" class="percent mandatory " onkeypress="blah(event)">
<input type="text" class="integer positive mandatory"
onkeypress="blah(event)">
<input type="text" class="integer positive optional"
onkeypress="blah(event)">
etc.


Very cool.... So I should be able to retrieve taborder values as well
using this method... And perhaps store general data like # of rows and
columns in a hidden element, so then I can derive most of my nav info....

Thanks,

--Yan
Jan 20 '06 #3

P: n/a
VK

Captain Dondo wrote:
So I should be able to retrieve taborder values as well
using this method...


For elements taborder there is native attribute "tabindex", and all
modern browsers (at least all you need to care about, including IE and
FF) respect this attribute:

<input type="text" name="t1" tabindex="1">
<input type="text" name="t2" tabindex="3">
<input type="text" name="t3" tabindex="2">
<input type="text" name="t4" tabindex="-1">

On tab press focus goes from t1 to t3, and only then to t2. The
negative value I used only on IE and it does what promised: such
element is excluded from taborder so requires an explicit mouse click
for focus.

Of course you need to use it only if you need to i) change the default
taborder or ii) exclude some elements from taborder.

And of course you can not only set tabindex but also read it:
var ti = someElement.tabIndex;

Jan 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.