473,795 Members | 2,986 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JS error: form.elements[...].checked is null or not an object

2 New Member
hi,

I am facing a js error in my code, below is the prob. statement

I have a radio2 javascript function as below, it works like this..
When a parent radio button is selected only one of its child radio button can be selected and on selection of a new child button(of the same parent) the earlier child button is deselected.

When i navigate to this particular page and select a parent and select a child, when i select another child of the same parent the radio is selected( this should not have been the behaviour) and the below error is displayed:
form.elements[...].checked is null or not an object.

The js pop-up says the below line is the error(line marked in bold in source code):
thisObj.form.el ements[parentRadioButt onName].checked=true;

My javascript code:
------------------



Expand|Select|Wrap|Line Numbers
  1. function sL_radio2_onclick(thisObj, thisEvent) {
  2. //use 'thisObj' to refer directly to this component instead of keyword 'this'
  3. //use 'thisEvent' to refer to the event generated instead of keyword 'event'
  4. // find the parent array number
  5. var indexStart = eval("form1:table1:".length);
  6. var indexEnd = thisObj.name.indexOf(':table2');
  7. var parentIndex = thisObj.name.substring(indexStart,indexEnd);
  8. // check the parent if child is checked and parent is not checked before
  9. parentRadioButtonName = 'form1:table1:'+parentIndex+':radio1';
  10. if (thisObj.checked && (!thisObj.form.elements[parentRadioButtonName].checked)) {
  11. thisObj.form.elements[parentRadioButtonName].checked=true;
  12. //uncheck other parent
  13. for(i=0;1==1;i++){
  14. parentRadioButtonName = 'form1:table1:'+i+':radio1';
  15. if (thisObj.form.elements[parentRadioButtonName]) {
  16. //if this parent is checked and not the parent of current child
  17. if(thisObj.form.elements[parentRadioButtonName].checked && (i != parentIndex)){
  18. thisObj.form.elements[parentRadioButtonName].checked=false;
  19. //uncheck children of this parent
  20. for(j=0;1==1;j++){
  21. childRadioButtonName = 'form1:table1:'+i+':table2:'+j+':radio2';
  22. if(thisObj.form.elements[childRadioButtonName]){
  23. thisObj.form.elements[childRadioButtonName].checked=false;
  24. } else {
  25. break;
  26. }
  27. }
  28. }
  29. } else {
  30. if (i>parentIndex) {
  31. break;
  32. }
  33. }
  34. }
  35.  
  36. }
  37. //if this child is checked, uncheck other children 
  38. // which has same parent as this child
  39. if (thisObj.checked) {
  40. for(i=0;1==1;i++){
  41. childRadioButtonName = 'form1:table1:'+parentIndex+':table2:'+i+':radio2';
  42. if(thisObj.form.elements[childRadioButtonName]){
  43. //check this child is not itself
  44. if (!(thisObj.form.elements[childRadioButtonName]==thisObj)) {
  45. thisObj.form.elements[childRadioButtonName].checked=false;
  46. }
  47. } else {
  48. break;
  49. }
  50. }
  51. }
  52. }
JSP call:
-------

Expand|Select|Wrap|Line Numbers
  1. <h:selectOneRadio styleClass="selectOneRadio" id="radio2"
  2. value="#{pc_SL.objectSpace.sPd}"  
  3. onclick="return sL_radio2_onclick(this, event);">
  4. <f:selectItem
  5. itemValue="#{varBObj.OrgBObj.sSId}"
  6. itemLabel=" " />
  7. </h:selectOneRadio>
Thanks in advance.
Nov 26 '07 #1
2 7321
Death Slaught
1,137 Top Contributor
Not completely sure what it is you're trying to do.

However if you're trying to get/manipulate the value of a radio button depending on wether it is checked or not, that I can do.

Here's an example:

[HTML]<html>

<head>
<script type="text/javascript">

function check() {
/* theCorrect is going to store the
correct answer's value */
/* theValue is going to store the
value of the user's input */
/* response is going to be the
computer's output */
var theCorrect;
var theValue;
var response;
//The for loop will check each radio button
/* i <= 3; may seem a little odd because there's
4 radio buttons, but the first rabio button is
treated as the 0th button,
the second radio as the 1rst,
and so on */
/* if one of the radio buttons is checked it gives
that value to the variable theValue */

for (i = 0; i <= 3; i++) {
if (document.myFor m.answer[i].checked == true) {
theValue=docume nt.myForm.answe r[i].value;
}
}

correct="4";

if (theValue == correct) {
response ="Congrats! You got it!";
document.myForm .txtResponse.va lue=response;
} else {
response ="Sorry," + " ";
response +=theValue;
response +=" " + "is not correct.";
document.myForm .txtResponse.va lue=response;
}

}

</script>
</head>

<body>

<p>
What's 2+2?
</p>

<form name="myForm">
<table border="1">

<tr>
<td>
<!-- by giving the radio buttons the same name,
only one can be checked at a time -->
<input type="radio" name="answer" value="1" />1
<input type="radio" name="answer" value="2" />2
<input type="radio" name="answer" value="3" />3
<input type="radio" name="answer" value="4" />4
</td>
</tr>

<tr>
<td>
<input type="button" onClick="check( )" value="Submit" />
</td>
</tr>

<tr>
<td>
<!-- This is where the computer's output will be,
you can do the same with with a textarea
as well -->
<input type="text" name="txtRespon se" />
</td>
</tr>

</table>
</form>
</body>

</html>[/HTML]

Ironicly I had the same error last night while working on a quiz program that's purely JavaScript and HTML. I forget now how I fixed it.

Hope it helps, Thanks, Death

PS - I remember what it was I did now. I had 6 radio buttons per question, and each set of buttons had their own name. The problem was in this line:

Expand|Select|Wrap|Line Numbers
  1. for (i = 0; i <= 6; i++) {
  2.  
  3. }
The problem as you might have guessed is in the "i <= 6;" section. This is telling the browser that there's 7 radio buttons, but I only had 6. I changed the line to "i <= 5;" and it worked like a charm.
Nov 26 '07 #2
mars123
2 New Member
Thanks for the reply.

What i want to do?
----------------------------
I have radio buttons corresponding to some id's retrieved from database.
Next each parent radio button has several child buttons(retriev ed from database).
When i select a parent button only one child button has to be selected and when i move from one child to another child radio button belonging to the same parent the previous child radio has to be de-selected automatically and next clicked radio to be selected.
When i select a parent radio only one parent radio has to be selected and when i move to another parent radio the previous parent has to be deselected and new parent radio selected.

P.N: The no. of radio's in a list depends on the database.

The proble lies in this line
Expand|Select|Wrap|Line Numbers
  1. if (thisObj.checked && (!thisObj.form.elements[parentRadioButtonName].checked)) {
When i debugged with IE debugger it shows the below values for the above statement:

thisObj.checked = true
(!thisObj.form. elements[parentRadioButt onName].checked) = error

Hope i was able to explain properly.
Nov 27 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
9997
by: Building Blocks | last post by:
Hi, All I need is a simle calculate form script which contains this: A script that can handle text input, radio buttons, checkboxes, and dropdowns. Each one of these variables will contain a number. That number will appear in a seperate box at the bottom. So basically whatever you choose has a corresponding number associated with it (except for the text input, which you enter whatever number) and those numbers are added and produced in...
2
1680
by: Edward | last post by:
The below code builds 2 tables 4 rows by 4 cols. All cells have checkboxes. When checked, the checkboxes in the first column automatically check the remainder of the check boxes in the same row. This is working fine for tables of this size. Unfortunately, my app produces lots of considerably bigger tables and the 'toggle' can take over a minute to complete!! I assume this is because for each toggle, the all form elements are searched...
7
2954
by: michael | last post by:
apologies in advance, as not only am i new to learning how to code javascript properly, i'm new to the groups posting thing... i am developing in firefox 1.0+, but will be working in an msie 6.0+ produciton environment ( not my choice, but when is it ever? ). the desired output is that when the end-user selects two radio buttons, one from each 'group', the form / page will open an alert window displaying the values of the radio buttons...
8
3784
by: Sean Shanny | last post by:
To all, The facts: PostgreSQL 7.4.0 running on BSD 5.1 on Dell 2650 with 4GB RAM, 5 SCSI drives in hardware RAID 0 configuration. Database size with indexes is currently 122GB. Schema for the table in question is at the end of this email. The DB has been vacuumed full and analyzed. Between 8 and 12 million records are added to the table in question each night. An analyze on the entire DB is done after the data has been loaded...
3
1303
by: Ed Jay | last post by:
When I run the below js, I get the error: "document.form1.elements.checked) is null or not an object" <script type="text/javascript"> function get_menu_value(menu_name) { var mValue = 0; for(var i=0; i < menu_name.length; i++) {
15
3345
by: judge82 | last post by:
I made sure I closed all the bracket, why do i still get the error. on line 360, 424, 425 and 607 Please help /* * ShopCartServletHW3.java *
1
1463
by: break9 | last post by:
I have a form that I uses javascript and <tr> tags to display or hide form fields. The problem is that Firefox won't acknowledge or pass any variables from any fields that were hidden when the page originally loaded. any help is appreciated. here is the javascript; var containerTag = 'TR'; var compatible = ( document.getElementById && document.getElementsByTagName && document.createElement && !(navigator.userAgent.indexOf('MSIE...
25
2734
by: premprakashbhati | last post by:
i want to have checkboxes in my gridview ...by checking it ,i can delete the row/rows.... so i have written code and got 2 Errors ...can anyone solve it... Error:1...'gridview.RetrieveItems()': not all code paths return a value ,and Error:2...No overload for method 'DeleteRows' takes '1' arguments Here is my aspx.cs Code: using System; using System.Data; using System.Configuration; using System.Collections;
0
9519
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10439
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10215
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10001
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9043
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6783
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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 we have to send another system

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.