Click to See Complete Forum and Search --> : How to reference textfield by ID instead of name


damon2003
11-14-2003, 07:00 AM
Hi,

how can I reference a textfield by ID instead of its name.

<input type="text" name="A. Catalogue"size="2" maxlength="2" id="cat">

i.e. to reference by name I would use the following
var a=document.form1.A. Catalogue.value;

It is easier to leave the name with spaces as it is as other scripts rely on it ( I didnt write the original)

I dont think this works because of the spaces or does anyone know how to reference this even with spaces. So how I reference this using the ID value?
thanks

requestcode
11-14-2003, 07:06 AM
Try:
var a=document.getElementById("cat").value

damon2003
11-14-2003, 07:09 AM
cool,
thanks a lot

Charles
11-14-2003, 07:11 AM
Danger, Will Robinson, Danger!

The "name" and "id" attributes of any HTML element share the same name space. That is, if you apply both to the same element then they must be the same.

damon2003
11-14-2003, 07:21 AM
I see,
so is it possible to reference an element when its name has spaces in it?

Charles
11-14-2003, 11:28 AM
From the HTML 4.01 Specification:
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").
http://www.w3.org/TR/html4/types.html#type-id