By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,472 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,472 IT Pros & Developers. It's quick & easy.

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

P: 2
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
Share this Question
Share on Google+
2 Replies


Death Slaught
100+
P: 1,137
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

P: 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

Post your reply

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