Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What am I doing wrong??? 1

Status
Not open for further replies.

prakus

Programmer
Aug 4, 2003
19
US
I have a set of records with a checkbox before each of them. Depending on a particular condition, there may be 2 radio buttons after each value(e.g. Record1). For records with radio buttons, I would like to ensure if the checkbox is checked for a record and then one of the following radio buttons should be checked too.

Thank you for your help in advance. Here is the code I have so far.

PK

<SCRIPT LANGUAGE='JavaScript'>

function checkEntry(form) {
var sel = false;
with (document.updateForm)
{
for (var i=0; i<elements.length; i++)
{
if(elements.type==checkbox && elements.checked==true)
{
if(elements[i+1].type==radio)
{
if(elements[i+1].checked==false && elements[i+2].checked==false)
{
alert("Please select an update option.")
elements[i+1].focus()
break
}
}
}
alert(elements.type)
break
}
}
if (sel == false)
{
return(false);}
}
</SCRIPT>

<form NAME="updateForm" METHOD="POST" ACTION="includes/update.asp" onSubmit="return checkEntry(this)">

<input type='checkbox' name=DBVar20 value= 'ON' >
Record1
<input type='radio' value='2' name=R_DBVar20 ><img border='0' src='images/blueStar.gif'>
<input type='radio' value='1' name=R_DBVar20 ><img border='0' src='images/downGreen.gif'>
<br>

<input type='checkbox' name=DBVar23 value= 'ON' >
Record2
<input type='radio' value='2' name=R_DBVar23 ><img border='0' src='images/blueStar.gif'>
<input type='radio' value='1' name=R_DBVar23 ><img border='0' src='images/upGreen.gif'>
<br>

<input type='checkbox' name=DBVar24 value= 'ON' >
Record3
<input type='radio' value='2' name=R_DBVar24 ><img border='0' src='images/blueStar.gif'>
<input type='radio' value='1' name=R_DBVar24 ><img border='0' src='images/upGreen.gif'>
<br>

<input type='checkbox' name=DBVar27 value= 'ON' >
Record4
<input type='radio' value='2' name=R_DBVar27 ><img border='0' src='images/blueStar.gif'>
<input type='radio' value='1' name=R_DBVar27 ><img border='0' src='images/upGreen.gif'>
<br>

<input type="button" value="Cancel" name="Cancel" >
<input type="submit" value="Update" name="updateForm">
</form>
 
...Here is the code I have so far.
Could you explain the problem? What have you tried so far? Have you tested this in any particular browser? Are you looking for a browser specific solution? If so, what browser (and OS)?

Cheers,
Jeff

[tt]Jeff's Blog [!]@[/!] CodeRambler
[/tt]

Make sure your web page and css validates properly against the doctype you have chosen - before you attempt to debug a problem!

FAQ216-6094
 
I am trying this in IE 6 and IE 7. The OS is Win XP. I am expecting the script to prevent submiting the form if no checkbox is checked or if some checkbox is checked which has a radio button (associated with the record) that is not checked. I am not able to achive either scenarios.

Thanks in advance,
PK.
 
I realize that I donot have anything in my function that returns true. But what I am not able to understand is why the form gets submitted when nothing is checked. Should that scenario not mean that the value of 'sel' is still 'false' and the form should not be submitted?

Any suggestions please.

Thank you,
PK
 
If you run this in Firefox and look at the error console, I bet you'll find the problem. I'd guess it has something to do with this line initially:
Code:
if(elements[i].type==checkbox && elements[i].checked==true)
though your next line of code (not counting the braces) should produce the same error.

Hint: Values of attributes are string values, not objects.

Lee
 
You're missing a few quotes around key elements in your code:
Code:
function checkEntry(form) {
var sel = false;
    with (document.updateForm)
        {
        for (var i=0; i<elements.length; i++)
            {
            if(elements[i].type==[!]"checkbox"[/!] && elements[i].checked)
                {
                if(elements[i+1].type==[!]"radio[/!])
                    {                        
                    if(!elements[i+1].checked && !elements[i+2].checked)
                        {
                        alert("Please select an update option.")
                        elements[i+1].focus()
                        break    
                        }
                    }
                }
            alert(elements[i].type)
            break            
            }    
        }
    if (!sel)
        {    
        return(false);}
        }

A side note that I'll point out, because it just causes needless clutter in your function (and moreso because it is a huge pet peeve of mine) .

You NEVER EVER EVER EVER have to put == true in a conditional. If the element contains the boolean value true, then a reference to the element in the conditional is all that is needed. The same thing holds for checking == false - it is much cleaner and quicker to put an ! in front of the element instead.

The 2 sets of code are functionally equivalent, but the 2nd is much tidier:
Code:
var a = true;
var b = false;

if (a == true) {
   alert("a is true");
}

if (b == false) {
   alert("b is false");
}

Code:
var a = true;
var b = false;

if (a) {
   alert("a is true");
}

if (!b) {
   alert("b is false");
}

I've applied this concept to your code above to make it cleaner.

One quick question about your code - will sel ever be a value other than false? Why are you doing the check at the bottom of the function? If sel is true, do you want to return true? If this is the case, why not just return sel instead of putting the conditional in there to make a redundant check?

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Lee, I believe you've "beaten me to the punch" for once! Guess I shouldn't have written a novel in my post, [lol]

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
You are right. I missed the quotes around the 'checkbox' and 'radio'.

Thanks for your help.

PK
 
...And thank you kaht for your suggestions/corrections too. I really appreciate them.

PK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top