473,379 Members | 1,330 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

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.myform.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 2226
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.myform.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.myform["2"];
temp[0].disabled=true;

If not, try debugging by using alert.
Like:
var temp=forms.myform["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.myform["2"];
temp[0].disabled=true;

If not, try debugging by using alert.
Like:
var temp=forms.myform["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.forms.myform["2"];

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

var temp=forms.myform["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*****@hotmail.com> wrote in message
news:pa****************************@hotmail.com...
Maybe stringnotation helps? (not sure)

var temp=forms.myform["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.myform["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#adef-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
Jon Hoowes wrote:
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...


Sorry for the mistake about the name not starting with a number. My bad! :)

An alternative - write a custom function which loops through all the form
elements, checking their .name property. When you find one that matches,
return it.

It wouldn't be super efficient, but it would probably do what you need.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Sep 15 '05 #11
Jon Hoowes wrote:
Maybe stringnotation helps? (not sure)

var temp=forms.myform["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 :(


So let's get this straight. You have a form with some sets of radio
buttons. You are given a number from some other function that you then
want to use as the name of a group of radio buttons.

So you may have a form with:

<form name="formA" ...>
<br>
<input type="radio" name="1" ...>
<input type="radio" name="1" ...>
<br>
<input type="radio" name="2" ...>
<input type="radio" name="2" ...>
</form>

You are given an number, say '2', and you want to disable the radio
buttons belonging to that group.

JavaScript does automatic type conversion, it seems that when you try to
access the element using '2' as a name it is converted to a number,
making it very hard to use.

So use the auto-type conversion to your advantage using an 'if' and
iterate over all the inputs and disable any that have a name that
matches 2.

'if' will type convert too, so it will turn the number back into a
string if it needs to.

<script type="text/javascript">

// Just a dummy function to return a number
function getNumber()
{
return 2;
}

function disableButtons( f )
{
var nameToUse = getNumber();
var el, els = f.getElementsByTagName('input');
var i = els.length;
while ( i-- ) {
el = els[i]
if ( 'radio' == el.type && nameToUse == el.name ) {
el.disabled = true;
}
}
}
</script>

<form name="formA" ...>
<br>
<input type="radio" name="1" ...>
<input type="radio" name="1" ...>
<br>
<input type="radio" name="2" value="2-1">
<input type="radio" name="2" value="2-2">
<br>
<input type="button" value="disable set 2" onclick="
disableButtons(this.form);
">
<input type="reset">
</form>

--
Rob
Sep 15 '05 #12

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

Similar topics

6
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...
3
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...
1
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...
11
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.