...

View Full Version : Checkbox value



rjkdonaldson
08-21-2007, 11:32 AM
Hi this is in response to my last question which was not replied to. Basically I'm trying to return a checkboxes value into the readonly text input. Here is my code for the latter all fuctioning well just this issue on da checkbox. This illustration used as demo for what is to be on my main form. Please help me on this one.

<HEAD>

<script type="text/javascript">

function checkBoxValidate(cb) {

for (j = 0; j < 4; j++) {
if (eval("document.myform.ckbox[" + j + "].checked") == true) {
document.myform.ckbox[j].checked = false;
if (j == cb) {
document.myform.ckbox[j].checked = true;
}
}

}
}

</script>
</HEAD>



<BODY>

<form name=myform>
Selection 1<input type="checkbox" name="ckbox" value"300" onClick="javascript:checkBoxValidate(0)"><br>
Selection 2<input type="checkbox" name="ckbox" value="200" onClick="javascript:checkBoxValidate(1)"><br>
Selection 3<input type="checkbox" name="ckbox" value="500" onClick="javascript:checkBoxValidate(2)"><br>
Selection 4<input type="checkbox" name="ckbox" value="100" onClick="javascript:checkBoxValidate(3)"><br>
<input type=text name=txt readonly onfocus "this.blur();">
</form>

</body>
</html>

mcjwb
08-21-2007, 01:38 PM
Try this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script type="text/javascript">
function checkBoxValidate(cb) {
for (j = 0; j < 4; j++) {
if (document.myform.ckbox[j].checked) {
document.myform.ckbox[j].checked = false;
if (j == cb) {
document.myform.ckbox[j].checked = true;
document.myform.txt.value = document.myform.ckbox[j].value;
}
}
}
}
</script>
</head>
<body>
<form name="myform">
Selection 1<input type="checkbox" name="ckbox" value="300" onclick="checkBoxValidate(0)"><br>
Selection 2<input type="checkbox" name="ckbox" value="200" onclick="checkBoxValidate(1)"><br>
Selection 3<input type="checkbox" name="ckbox" value="500" onclick="checkBoxValidate(2)"><br>
Selection 4<input type="checkbox" name="ckbox" value="100" onclick="checkBoxValidate(3)"><br>
<input type="text" name="txt" readonly>
</form>
</body>
</html>


Also I've got rid of the use of the eval() function which you don't need and added a few missing equals signs!

Is there a reason why you're not using radio buttons instead of checkboxes?

rjkdonaldson
08-21-2007, 01:51 PM
I'm not using radio buttons for one simple reason even though its da norm, coz its more of a catch i reckon to use checkboxes but with radio buttons they are default only one can be chosen. Been searching for a way just to return the value of the box into readonly input, other parts of my actual code working though.Thanks anyway.

rjkdonaldson
08-21-2007, 01:57 PM
thanks bro works sweet as.

mcjwb
08-21-2007, 01:58 PM
No problem.

amitthechosen1
08-22-2007, 05:54 AM
Hi,
Pasted below is a possible solution to your problem.
However, the first checkbox displays ON in my browser, instead of the values.
rest is working fine. I have only changed the function.

<HEAD>

<script type="text/javascript">


function checkBoxValidate(cb) {
var value =0 ;
/*

//alert(document.myform.ckbox[cb].checked);

if(document.myform.ckbox[cb].checked == true) {

for(i=0;i<4;i++){

if(document.myform.ckbox[i].checked == true) {

value = value + document.myform.ckbox[i].value;

}
}

//alert(value);
//value = document.myform.ckbox[cb].value;
document.myform.txt.value = value;

}
//if it is unchecked, we need to replace the existing ones
else {
value =0 ;
for(i=0;i<4;i++){

if(document.myform.ckbox[i].checked == true) {

value = value + document.myform.ckbox[i].value;

}
}

if(value == 0){
document.myform.txt.value = "";
}
else {

document.myform.txt.value = value;
}
}
*/

for(i=0;i<4;i++){

if(document.myform.ckbox[i].checked == true) {

value = value + document.myform.ckbox[i].value;

}
}

var len = value.length;
var i=0;
var str="";
for(i = 1;i<len;i++)
{
str = str + value.charAt(i);

}
//alert(str);

document.myform.txt.value = str;
}
</script>
</HEAD>

<BODY>

<form name=myform>
Selection 1<input type="checkbox" name="ckbox" value"300" onClick="javascript:checkBoxValidate(0)"><br>
Selection 2<input type="checkbox" name="ckbox" value="200" onClick="javascript:checkBoxValidate(1)"><br>
Selection 3<input type="checkbox" name="ckbox" value="500" onClick="javascript:checkBoxValidate(2)"><br>
Selection 4<input type="checkbox" name="ckbox" value="100" onClick="javascript:checkBoxValidate(3)"><br>
<input type=text name=txt readonly >
</form>

</body>
</html>

mcjwb
08-22-2007, 04:40 PM
Thanks for the input amit, but I don't think he was after a script that added up the selected check boxes.
Also I'd like to draw your attenttion to the parseInt() and parseFloat() functions, you may find them useful!

Oh and the reason the first checkbox results in "ON" being displayed is because there is an equals sign missing from the first checkbox's value attribute.

rjkdonaldson
09-03-2007, 10:09 AM
thx for the help got my radio buttons to function. Just the alert message I added to it later to alert user to make a selection messed up kept cycling through the error message even after checkbox had been checked.
tried pasting the alert message in different parts of script and error message kept popping up so I kinda flagged the idea of alerting user to make a selection.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum