View Full Version : checkboxes and names and functions
scroots
10-22-2002, 10:30 PM
i have a series of checkboxes generated dynamically each with a different name e.g. 1 or 271 in no fixed order.
when a checkbox is checked i would like a confirm box to apear asking to proceed, and if yes is clicked the user will be redirected to a page depending on what the checkbox is called
so checkbox 34 is checked they click yes in the confirm box and they are redirected to http://www.mysite.com/blah.htm?id=34
i know one long way of doing it to have a script for every checkbox, but this is not very efficent is there a faster, more efficent way?
thanks in advance
scriits
beetle
10-23-2002, 01:05 AM
If the numbered checkboxes of which you speak the only checkboxes on the page...then this solution should work for you.<script>
function checkClick(e) {
var cb = (document.all) ? e.srcElement : e.target;
if (cb.type != "checkbox") return;
if (cb.checked)
if (confirm("Proceed?"))
top.location.href = 'blah.htm?id='+cb.id;
else
return;
}
</script>
<body onClick="checkClick(event);">
whammy
10-23-2002, 02:22 AM
This sounds problematic by your description... are you sure you shouldn't be using radio buttons?
If you are using checkboxes and have to pass multiple values which are dynamically generated, then I have a solution for you as well.
I actually never had to deal with that until yesterday, but there's a fairly simple solution. ;)
scroots
10-23-2002, 06:25 PM
only one checkbox will be checked at a time, so it isn`t that problemmatic.
thanks beetle and whammy.
scroots
scroots
10-27-2002, 08:02 PM
how could i alter the above script so i check the check box and then click a button, then it redirects i have tried removing
? e.srcElement : e.target;
but then the script produces an error.
scroots
RadarBob
10-28-2002, 03:30 PM
So you're creating n number of checkboxes dynamically and you want to scan that set, yes?
How about naming all the checkboxes the same. Then you can reference them like an array - just like you would if you were using radio buttons.
The code sample is written so that if multiple boxes are checked, the last one checked will be the one.
<script type="text/javascript">
function goThere (ChkBoxGrp) {
var newLocation = new String();
for (var x=0; i<ChkBoxGrp.length; x++) {
if (ChkBoxGrp[x].checked == true) {
newLocation = ChkBoxGrp[x].value;
}
}
window.location = newLocation;
}// function goThere()
</script>
. . .
<form name="myForm"...>
<input type="checkbox" name="ChkBoxGrp" value="http://www.apple.com"
onclick="goThere(myForm.ChkBoxGrp);"> An apple a day keeps Windows at bay.
<!-- repeat checkboxes as necessary -->
</form>
If you really want to make sure only one checkbox is checked, then I suggest you use radio buttons. Otherwise you could put in some code that simply checks for more than one of the set being checked.
RadarBob
10-28-2002, 03:41 PM
Originally posted by scroots
only one checkbox will be checked at a time, so it isn`t that problemmatic.
Famous last words in interface design-rationalization;)
scroots
10-28-2002, 06:11 PM
that would work, but for each checkbox i have i dont want to have two lines of code to go with it.
scroots
RadarBob
10-28-2002, 06:27 PM
:confused:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.