Working on forms where element names have spaces

P

PeteC

I'm working on a web page which has some form elements in it which I need to
manipulate using JavaScript.

If the element is (for example, in a form called TicketForm)

<INPUT name="intNumTickets" onBlur="calcPrice();" value="0">

I can read and manipulate this value in CalcPrice using the format

document.TicketForm.intNumTickets

My problem is, this form needs to pass the values to a payment processor,
and they say that the name needs to be preceded with the text 'PT' so that
it is passed back to the form later without any processing (no, I cannot
change this aspect).

So the input statement would be

<INPUT name="PT intNumTickets" onBlur="calcPrice();" value="0">

....but my problem is, how do I reference PT intNumTickets using the document
format?

document.TicketForm.PT intNumTickets

won't work.

Any ideas how I can handle this?

Regards,


Pete.
 
Y

Yann-Erwan Perio

PeteC said:
<INPUT name="PT intNumTickets" onBlur="calcPrice();" value="0">

...but my problem is, how do I reference PT intNumTickets using the document
format?

You use the square brackets notation, i.e.
document.forms["myForm"].elements["PT intNumTickets"];

See the FAQ, especially
<URL:http://jibbering.com/faq/#FAQ4_39>


Regards,
Yep.
 
W

web.dev

Hi Pete,
they say that the name needs to be preceded with the text 'PT'... [snip]
.. name="PT intNumTickets" ...

First of all, they said that it needed to preceded with 'PT', but did
they also say it had to have a space in between? If that is still the
case, then yes there is another way for you to reference the form
elements. Try using the following way:

document.forms[x].elements[y]

For example, let's say you have only 1 form:

<form name = "foo" action = "url" method = "post" onsubmit = "return
myFunc();">
<input name = "PT intAbc"/>
<input name = "PT intNumTickets"/>
<input name = "PT intXyz"/>
</form>

You can then go through your elements by the following:

for(var i = 0; i < 3; i++)
{
var tmp = document.forms[0].elements.value;
//code to do something with the value
}

Where 3 (if you know beforehand) is the number of elements in the form.

Hope this helps. :)
 
Z

Zif

Danny said:
Spaces are not allowed in ID/Names per standards:

HTML4.1 and CSS definition on CDATA which is what name/attributes use

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 (".").

That is *not* the specification for CDATA, it is for SGML ID and NAME
tokens (that's why it says ID and NAME tokens in the text).

The CDATA spec is:

" CDATA is a sequence of characters from the document character set and
may include character entities. User agents should interpret attribute
values as follows:

* Replace character entities with characters,
* Ignore line feeds,
* Replace each carriage return or tab with a single space.

"User agents may ignore leading and trailing white space in CDATA
attribute values (e.g., " myval " may be interpreted as "myval").
Authors should not declare attribute values with leading or trailing
white space.

"For some HTML 4 attributes with CDATA attribute values, the
specification imposes further constraints on the set of legal values
for the attribute that may not be expressed by the DTD.

"Although the STYLE and SCRIPT elements use CDATA for their data model,
for these elements, CDATA must be handled differently by user agents.
Markup and entities must be treated as raw text and passed to the
application as is. The first occurrence of the character sequence "</"
(end-tag open delimiter) is treated as terminating the end of the
element's content. In valid documents, this would be the end tag for
the element."

None of which disallows the use of spaces.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top