Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > how to access form elements with numbers as names

Reply
Thread Tools

how to access form elements with numbers as names

 
 
Jon Hoowes
Guest
Posts: n/a
 
      09-14-2005
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
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      09-14-2005
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
 
Reply With Quote
 
 
 
 
Erwin Moller
Guest
Posts: n/a
 
      09-14-2005

> 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
 
Reply With Quote
 
Jon Hoowes
Guest
Posts: n/a
 
      09-14-2005

> 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
 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      09-14-2005
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


 
Reply With Quote
 
David Dorward
Guest
Posts: n/a
 
      09-14-2005
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
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      09-14-2005
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.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      09-15-2005
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
 
Reply With Quote
 
Jon Hoowes
Guest
Posts: n/a
 
      09-15-2005
> 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
 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      09-15-2005
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
newb: recurse over elements children and disable all form elements SteveKlett@gmail.com Javascript 4 09-20-2006 07:42 AM
Re: Valid form element names? How to use ID instead of name? Processing form data Michael Winter HTML 4 03-06-2004 10:21 PM
Re: Valid form element names? How to use ID instead of name? Processing form data Robert HTML 0 03-06-2004 07:31 PM
Re: Valid form element names? How to use ID instead of name? Processing form data Jukka K. Korpela HTML 3 03-06-2004 04:30 PM



Advertisments