problems with js onchange and radios

D

doc

Hi,


<SCRIPT LANGUAGE="JavaScript">
function check_selection(selection){
document.pr.n_o[selection][0].checked=true;
}
</SCRIPT>

<form name=pr action=test.php method=post>
<input type=radio name="n_o[2]" value="yes">yes
<select name="n_w[2]" onChange="check_selection(2)" >
.....
</select>
</form>

after changing select object i'm receiving error:
Error 'document.pr.n_o' is null or not an object.
Do you have any idea's how to fix it?

__
doc.
 
M

Michael Winter

<SCRIPT LANGUAGE="JavaScript">

The language attribute is deprecated. Use the (required) type
attribute instead:

function check_selection(selection){
document.pr.n_o[selection][0].checked=true;

There will be no 'n_o' property of the 'pr' object. The proper way to
go about that would be:

document.forms['pr'].elements[
'n_o[' + selection + ']'
][0].checked = true;

Notice that string concatenation is used to create the name, then used
to access the INPUT element.

I assume there is more than one 'n_o[2]' form control, so I've left
the [0] subscript in. If there's only one such control, remove that.

If this identification scheme is consistent (that is, n_w[1] changes
n_o[1], and n_w[3] changes n_o[3], etc.) whenever check_selection is
called, you could simplify the call a bit:

function checkSelection(s) {
s.form.elements[
'n_o[' + /\[(\d+)\]/.exec(s.name)[1] + ']'
][0].checked = true;
}

<select ... onchange="checkSelection(this);">

[snip]

Hope that helps,
Mike
 
R

RobG

doc said:
Hi,


<SCRIPT LANGUAGE="JavaScript">

The language attribute is depreciated, type is required:

function check_selection(selection){
document.pr.n_o[selection][0].checked=true;

This seems to be trying to refer to a form with a name 'pr', an
element collection with a name 'n_o' and element index 'selection'
which itself is a collection... and that's where your trouble is.

If you want to use the syntax you have constructed, then:

document.pr.elements['n_o[' + selection + ']'].checked = true;

will do the trick. A more robust version is:

document.forms['pr'].elements['n_o[' + selection + ']'].checked=true;

I'd suggest not using square brackets this way, use some other
character such as '-', '.' or '_' to separate your pseudo-index from
the name.

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

}
</SCRIPT>

<form name=pr action=test.php method=post>

While it is not mandatory to quote attributes, it is always a good
idea and (AFAIKR) all examples the W3C HTML spec use quotes:

<input type=radio name="n_o[2]" value="yes">yes
<select name="n_w[2]" onChange="check_selection(2)" >

When passing a digit that is to be used as a string, it may be better
to pass it as a string, although in this case it doesn't matter but
it may make it more obvious to someone else trying to maintain your
code:

.....
</select>
</form>

after changing select object i'm receiving error:
Error 'document.pr.n_o' is null or not an object.
Do you have any idea's how to fix it?

Working version:

<script type="text/javascript">
function check_selection(selection){
document.pr.elements['n_o[' + selection + ']'].checked = true;
}
</script>

<form name="pr" action="">
<input type=radio name="n_o[2]" value="yes">yes
<select name="n_w[2]" onChange="check_selection('2')">
<option>one</option>
<option>two</option>
</select>
<input type="reset">
</form>
 
M

Michael Winter

On 18/04/2005 12:00, RobG wrote:

[snip]
I'd suggest not using square brackets this way, use some other
character such as '-', '.' or '_' to separate your pseudo-index from
the name.

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

<URL:http://www.w3.org/TR/html4/types.html#type-cdata>

The name attribute for form controls has a CDATA value, not ID or
NAME. The form used by the OP is perfectly acceptable.

Mike


Only META elements have a name attribute that is of NAME type. The
only other attribute with type NAME, http-equiv, also belongs to the
META element.

The only attribute of ID type is, as you would expect, the id
attribute. However, the for attribute of LABEL, and the headers
attribute of TD and TH, are of type IDREF and IDREFS, respectively. As
these contain ID values, they too are limited to ID characters (though
IDREFS allows spaces a delimiter).
 
R

RobG

Michael said:
On 18/04/2005 12:00, RobG wrote:

[snip]
I'd suggest not using square brackets this way, use some other
character such as '-', '.' or '_' to separate your pseudo-index from
the name.

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

<URL:http://www.w3.org/TR/html4/types.html#type-cdata>


The name attribute for form controls has a CDATA value, not ID or NAME.
The form used by the OP is perfectly acceptable.

Mike


Only META elements have a name attribute that is of NAME type. The only
other attribute with type NAME, http-equiv, also belongs to the META
element.

The only attribute of ID type is, as you would expect, the id attribute.
However, the for attribute of LABEL, and the headers attribute of TD and
TH, are of type IDREF and IDREFS, respectively. As these contain ID
values, they too are limited to ID characters (though IDREFS allows
spaces a delimiter).

One day I will work out how to understand the spec...

For the record, the spec says that an input must have a name
attribute to be successful (they're kinda useful for other things
too). Names are cdata and a link is given to the URL above.

I now realise that the reference to ID and NAME tokens listed just
below CDATA actually refers to SGML basic types and not HTML element
attributes. The fact that the page is headed 'HTML Basic Types' just
added to the confusion - the semantics of /type/ versus /attribute/
plumb evaded me... now there's a surprise.
 
M

Michael Winter

Michael Winter wrote:
[snip]
Only META elements have a name attribute that is of NAME type. The
only other attribute with type NAME, http-equiv, also belongs to the
META element.

Oops. I forgot to check the entities, wherein %LanguageCode; is NAME,
and applies to hreflang (A, LINK), and lang (various).

[snip]
One day I will work out how to understand the spec...

When you look at the details of each attribute, you'll see this
general format:

<attr> = <type> [<case>]

In other words, the attribute name, followed by its type, and an
abbreviation that indicates if and how case is significant. For example,

id = name [CS]

The id attribute is of type NAME, and is Case-Sensitive.

type = content-type [CI]

The type attribute is of type %ContentType; (an entity that expands to
CDATA), and is Case-Insensitive.

style = style [CN]

The style attribute is of type %StyleSheet; (an entity that expands to
CDATA), and is Case-Neutral[1].

[snip]

Mike


[1] Case-neutral isn't necessarily the same as case-insensitive.
CN tends to be used in situations where the notion of case is
meaningless, for example when numbers or punctuation might be
a value.
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top