473,569 Members | 2,481 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to access form elements with numbers as names

Hi,

I have inherited some code that has some form elements (radio buttons)
that are called "1", "2" etc.

for example:

<input name="2" type="radio" value="45">
<input name="2" type="radio" value="46">
I want to be able to access them through javascript to disable them;

forms.myform.2[0].disabled=true;

etc.
however I get a javascript error when I try to do this (something about a
missing semicolon).
My javascript skills are fine and I have other elements being disabled OK,
so I assume that this is a problem with the way that the elements were
named.

Unfortunately I don't have the option of renaming the elements. Is there
another way to access these elements?
I have tried:

var temp=forms.myfo rm.2;
temp[0].disabled=true;

But I get the same missing semicolon error on the "var temp=..." line

Any thoughts appreciated!

Cheers,

Ben
Sep 14 '05 #1
11 2243
Jon Hoowes wrote:
Hi,

I have inherited some code that has some form elements (radio buttons)
that are called "1", "2" etc.

for example:

<input name="2" type="radio" value="45">
<input name="2" type="radio" value="46">
I want to be able to access them through javascript to disable them;

forms.myform.2[0].disabled=true;

etc.
however I get a javascript error when I try to do this (something about a
missing semicolon).
My javascript skills are fine and I have other elements being disabled OK,
so I assume that this is a problem with the way that the elements were
named.

Unfortunately I don't have the option of renaming the elements. Is there
another way to access these elements?
I have tried:

var temp=forms.myfo rm.2;
temp[0].disabled=true;

But I get the same missing semicolon error on the "var temp=..." line

Any thoughts appreciated!

Cheers,

Ben


Hi Ben,

Maybe stringnotation helps? (not sure)

var temp=forms.myfo rm["2"];
temp[0].disabled=true;

If not, try debugging by using alert.
Like:
var temp=forms.myfo rm["2"];
alert (tmp);
temp[0].disabled=true;

is tmp pointing to a radio-object?

And yes: Strange design to name your elements like that. :-/

Regards,
Erwin Moller
Sep 14 '05 #2
Hi Ben,

Maybe stringnotation helps? (not sure)

var temp=forms.myfo rm["2"];
temp[0].disabled=true;

If not, try debugging by using alert.
Like:
var temp=forms.myfo rm["2"];
alert (tmp);
temp[0].disabled=true;

is tmp pointing to a radio-object?

And yes: Strange design to name your elements like that. :-/

Regards,
Erwin Moller


One more thing, I always use document too before adressing elements.
So:
var temp=document.f orms.myform["2"];

Regards,
Erwin Moller
Sep 14 '05 #3
Maybe stringnotation helps? (not sure)

var temp=forms.myfo rm["2"];
temp[0].disabled=true;
This works, but doesn't allow me to access the elements by their "name"
And yes: Strange design to name your elements like that. :-/


It's a system that builds a form based on database information. The
primary key, which is a number, determines the name of the element.

This is also why the suggestion you made is not suitable - it works, but I
cannot guarantee the order of each element within the form :(

Cheers anyway!

Jon
Sep 14 '05 #4
Jon Hoowes wrote:
I have inherited some code that has some form elements (radio buttons)
that are called "1", "2" etc.


The core problem is the fact that form element names cannot begin with a
number.
It's invalid HTML.
Therefore, your javascript cannot be expected to work.

You must rename the form elements.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 14 '05 #5

"Jon Hoowes" <ju*****@hotmai l.com> wrote in message
news:pa******** *************** *****@hotmail.c om...
Maybe stringnotation helps? (not sure)

var temp=forms.myfo rm["2"];
temp[0].disabled=true;


This works, but doesn't allow me to access the elements by their "name"
And yes: Strange design to name your elements like that. :-/


It's a system that builds a form based on database information. The
primary key, which is a number, determines the name of the element.


make the name of the element up using a string AND the primary key. ie

"element" + primaryKey...
Sep 14 '05 #6
Matt Kruse wrote:
The core problem is the fact that form element names cannot begin with a
number.


NAME tokens cannot start with a number, but the value of the name attribute
is not a NAME token. Form control names may begin with a number.

--
David Dorward <http://blog.dorward.me .uk/> <http://dorward.me.uk/>
Home is where the ~/.bashrc is
Sep 14 '05 #7
Lee
Jon Hoowes said:

Maybe stringnotation helps? (not sure)

var temp=forms.myfo rm["2"];
temp[0].disabled=true;


This works, but doesn't allow me to access the elements by their "name"
And yes: Strange design to name your elements like that. :-/


It's a system that builds a form based on database information. The
primary key, which is a number, determines the name of the element.


It would be fine to allow the numeric key to *determine* the name
of the element, but it was a bad decision to allow the numeric key
to *be* the name of the element. Surely your developers can figure
out how to translate the key "2" into "element_2" , or some other
valid name.

Sep 14 '05 #8
David Dorward wrote:
Matt Kruse wrote:

The core problem is the fact that form element names cannot begin with a
number.

NAME tokens cannot start with a number, but the value of the name attribute
is not a NAME token. Form control names may begin with a number.


Yes, the value of an HTML name attribute is CDATA. The ID attribute is
a NAME token and can't start with a number - easily confused!

Name:
<URL:http://www.w3.org/TR/html4/interact/forms.html#adef-name-INPUT>

ID:
<URL:http://www.w3.org/TR/html4/struct/global.html#ade f-id>

--
Rob
Sep 15 '05 #9
> It would be fine to allow the numeric key to *determine* the name
of the element, but it was a bad decision to allow the numeric key
to *be* the name of the element. Surely your developers can figure
out how to translate the key "2" into "element_2" , or some other
valid name.


I agree 100%, but I don't have any way to update the code that takes the
script - it is on a separate system and although I have asked them to
change it, I don't think they want to, hence wondering if there is anyway
to access the element without changing the element name...

Cheers,

Jon
Sep 15 '05 #10

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

Similar topics

6
5012
by: tm | last post by:
I am trying to reference a table entry (qtyonhand) populated from a recordset. There is only one record displayed on this table. When i try to compare this displayed field to an input field (orderqty) i get an (error on this page or no result at all ). When just comparing the input field to a set of "" everything works fine. This is an ASP...
3
3104
by: Megan | last post by:
I am not that good at Access and don't know much about it, so I have a few questions for anyone who knows Access very well. First off, I should mention that the company I work for uses Access as a Rolodex for names, addresses, and phone numbers of clients. Okay, first question: When I open Access and click on Enter/View Contacts, how do I...
1
1519
by: kmnotes04 | last post by:
I have a data entry form that contains drop-down lists such as 'Assigned to:' with a list of staff member names. Those names end up as numerical codes in the main table ('ProjectInfo') of the Access database. Is there some way - such as a SQL satement added to a query already set up in Access - to add a statement such as 'if 1 then 'Smith',...
11
2977
by: Rik | last post by:
Hello guys, now that I'm that I'm working on my first major 'open' forms (with uncontrolled users I mean, not a secure backend-interface), I'd like to add a lot of possibilities to check wether certain fields match certain criteria, and inform the user in different ways when the data is wrong (offcourse, this will be checked on posting the...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7926
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. ...
0
8132
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...
1
7678
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...
0
7982
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
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...
0
5222
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...
0
3656
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...
0
944
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.