473,466 Members | 1,333 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

problems with js onchange and radios

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.
Jul 23 '05 #1
5 1737
On 18/04/2005 11:00, doc wrote:
<SCRIPT LANGUAGE="JavaScript">
The language attribute is deprecated. Use the (required) type
attribute instead:

<script type="text/javascript">
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

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #2
doc wrote:
Hi,
<SCRIPT LANGUAGE="JavaScript">
The language attribute is depreciated, type is required:

<script type="text/javascript">
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 (".")."

<URL:http://www.w3.org/TR/html4/types.html#type-cdata>
}
</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:

<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)" >
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 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?


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>

--
Rob
Jul 23 '05 #3
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).

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #4
Michael Winter wrote:
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.
--
Rob
Jul 23 '05 #5
On 19/04/2005 00:47, RobG wrote:
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.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
Jul 23 '05 #6

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

Similar topics

3
by: daveland | last post by:
I am working on some JavaScript that dynamically adds rows to a table in response to a button click. A new row does appear on the screen when the button is clicked. However, that table to which a...
2
by: Andy Goldstein | last post by:
I have a table where all the TRs have an onClick handler registered. One (and only one) of the rows has 2 text input boxes, where each textbox has an onChange handler registered. Both the onClick...
5
by: Shawn Modersohn | last post by:
For the script: <script language="JavaScript"> function pullPage(){ var arrayLength=document.teamSelectionF.teamSelectionS.length; var pageNav = new Array(arrayLength); var...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
2
by: DonD | last post by:
I have a form that allows a user to upload a file. When they select a file (onChange), I call a JS function that analyzes the filename for specific keywords and then populates a multiple select box...
4
by: Angela | last post by:
Hi everyone, At the moment I am using the syntax of: <asp:radiobuttonlist id="rdDrive" runat="server" RepeatLayout="Table" RepeatColumns="2"> <asp:ListItem Value="1"...
6
by: Good Man | last post by:
Hi there I'm cloning DOM elements on the fly, and I need to assign different "onchange" attributes. As many others have discovered and documented, the following works with Mozilla but not IE: ...
9
by: mantrid | last post by:
hello In the function below radtext is an array of 3 radio buttons with values set to 1, 2 and 3. but variables starty and finishy are not returned. ...
3
by: KimberlyM | last post by:
This has been driving me crazy. I hope someone can help. The site displays perfectly in FF but all div's do not show in IE. Please help me find the problem! Thanks! Here is my css. ...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.