473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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.elements[parentRadioButtonName].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 7283
Death Slaught
1,137 1GB
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.myForm.answer[i].checked == true) {
theValue=document.myForm.answer[i].value;
}
}

correct="4";

if (theValue == correct) {
response ="Congrats! You got it!";
document.myForm.txtResponse.value=response;
} else {
response ="Sorry," + " ";
response +=theValue;
response +=" " + "is not correct.";
document.myForm.txtResponse.value=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="txtResponse" />
</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
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(retrieved 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[parentRadioButtonName].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
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...
2
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. ...
7
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+...
8
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...
3
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; ...
15
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
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...
25
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.