daimo1
asked on
Graphical Checkbox - adding a toggle function.
Have the following javascript that enhances all the text boxes on a page to graphical ones. (enhanceCheckboxes).
This works a treat. Now want to be able to toggle the checkboxes to 'Check all' or 'Uncheck all' or have a certain ones checked or unchecked. Have been able to do this without the enhanced checkboxes (checkAll, CheckDefault) but am having trouble combining the 'checkAll' and 'CheckDefault' with 'enhanceCheckboxes'. Any ideas?
Thanks
D
__________________________ __________ ___
function enhanceCheckboxes() {
// finds every checkbox on the page
// and converts it to a graphical version
// find all input controls:
var els=document.getElementsBy TagName("i nput");
// for each input element...
for(var i=0; i < els.length; i++) {
if (els[i].type=='checkbox') { // its a checkbox
// hide the original checkbox control:
els[i].style.display='none ';
// create the graphical alternative:
var img = document.createElement("im g");
// initial state of graphical checkbox
// is the same as the original checkbox:
if (els[i].checked) {
img.src="images/checked.gi f";
img.title="Checked";
}
else {
img.src="images/unchecked. gif";
img.title="Unchecked";
}
// assign our onclick event
img.onclick= toggleCheckbox;
// insert the new, clickable image into the DOM
// infront of the original checkbox:
els[i].parentNode.insertBe fore(img, els[i]);
}
}
}
function toggleCheckbox() {
// graphical checkbox onclick event handler
// toggle the checkbox state:
if (this.title == "Checked") {
// toggle the image and title:
this.src="images/unchecked .gif";
this.title="Unchecked";
// update the hidden real checkbox to match the state of the graphical
// version:
this.nextSibling.checked=f alse;
}
else {
// toggle the image and title:
this.src="images/checked.g if";
this.title="Checked";
// update the hidden real checkbox to match the state of the graphical
// version:
this.nextSibling.checked=t rue;
}
}
__________________________ __________ __________ ______
function checkAll(f,check){
for(i=0;i<f.elements.lengt h;i++)
if (f.elements[i].type=='chec kbox')
if (f.elements[i].checked!=ch eck) f.elements[i].checked=chec k
}
Used with two buttons:
<input type="button" value="Check All" onClick="checkAll(this.for m,true)">
<input type="button" value="Uncheck All" onClick="checkAll(this.for m,false)">
__________________________ __________ __________ _______
function CheckDefault(){
document.Form1.box1.checke d = true;
document.Form1.box2.checke d = false;
document.Form1.box3.checke d = false;
document.Form1.box4.checke d = true;
Used with:
<input type="button" value="Default" onClick="CheckDefault();">
This works a treat. Now want to be able to toggle the checkboxes to 'Check all' or 'Uncheck all' or have a certain ones checked or unchecked. Have been able to do this without the enhanced checkboxes (checkAll, CheckDefault) but am having trouble combining the 'checkAll' and 'CheckDefault' with 'enhanceCheckboxes'. Any ideas?
Thanks
D
__________________________
function enhanceCheckboxes() {
// finds every checkbox on the page
// and converts it to a graphical version
// find all input controls:
var els=document.getElementsBy
// for each input element...
for(var i=0; i < els.length; i++) {
if (els[i].type=='checkbox') { // its a checkbox
// hide the original checkbox control:
els[i].style.display='none
// create the graphical alternative:
var img = document.createElement("im
// initial state of graphical checkbox
// is the same as the original checkbox:
if (els[i].checked) {
img.src="images/checked.gi
img.title="Checked";
}
else {
img.src="images/unchecked.
img.title="Unchecked";
}
// assign our onclick event
img.onclick= toggleCheckbox;
// insert the new, clickable image into the DOM
// infront of the original checkbox:
els[i].parentNode.insertBe
}
}
}
function toggleCheckbox() {
// graphical checkbox onclick event handler
// toggle the checkbox state:
if (this.title == "Checked") {
// toggle the image and title:
this.src="images/unchecked
this.title="Unchecked";
// update the hidden real checkbox to match the state of the graphical
// version:
this.nextSibling.checked=f
}
else {
// toggle the image and title:
this.src="images/checked.g
this.title="Checked";
// update the hidden real checkbox to match the state of the graphical
// version:
this.nextSibling.checked=t
}
}
__________________________
function checkAll(f,check){
for(i=0;i<f.elements.lengt
if (f.elements[i].type=='chec
if (f.elements[i].checked!=ch
}
Used with two buttons:
<input type="button" value="Check All" onClick="checkAll(this.for
<input type="button" value="Uncheck All" onClick="checkAll(this.for
__________________________
function CheckDefault(){
document.Form1.box1.checke
document.Form1.box2.checke
document.Form1.box3.checke
document.Form1.box4.checke
Used with:
<input type="button" value="Default" onClick="CheckDefault();">
Hi
What trouble you are facing?
Nab
What trouble you are facing?
Nab
Hi daimo1,
Try this:
__________________________ __________ __________ ______
function checkAll(f,check){
for(i=0;i<f.elements.lengt h;i++)
if (f.elements[i].type=='chec kbox')
if (f.elements[i].checked!=ch eck) {
f.elements[i].checked=chec k;
f.elements[i].previousSibl ing.title= "Checked";
}
}
__________________________ __________ __________ _______
function CheckDefault(){
document.Form1.box1.checke d = true;
document.Form1.box1.previo usSibling. title="Che cked";
document.Form1.box2.checke d = false;
document.Form1.box2.previo usSibling. title="Unc hecked";
document.Form1.box3.checke d = false;
document.Form1.box3.previo usSibling. title="Unc hecked";
document.Form1.box4.checke d = true;
document.Form1.box4.previo usSibling. title="Che cked";
}
__________________________ __________ __________ _______
Peace and joy. mvan
Try this:
__________________________
function checkAll(f,check){
for(i=0;i<f.elements.lengt
if (f.elements[i].type=='chec
if (f.elements[i].checked!=ch
f.elements[i].checked=chec
f.elements[i].previousSibl
}
}
__________________________
function CheckDefault(){
document.Form1.box1.checke
document.Form1.box1.previo
document.Form1.box2.checke
document.Form1.box2.previo
document.Form1.box3.checke
document.Form1.box3.previo
document.Form1.box4.checke
document.Form1.box4.previo
}
__________________________
Peace and joy. mvan
@Nab, Hi, and congratulations! (on JavaScript Master Level: 56040 Expert Points) Nice job, especially in less than a month!
Peace and joy. mvan
Peace and joy. mvan
ASKER
Hi Nab
The code change made no difference.
Without the graphical checkboxes (enhanceCheckboxes) script both the checkAll, CheckDefault scripts work fine. With the graphical checkboxes, the enhanceCheckboxes script works but the buttons for checkAll, CheckDefault don't make any difference to the graphical checkboxes.
The graphical checkboxes script converts the normal checkbox code in order to show the graphical checkboxes to the user.
The checkAll and CheckDefault scripts are working but they don't affect the graphical checkboxes, but as this cannot be seen by the user it makes little. What I need is enhancement to the checkAll and CheckDefault scripts to allow for the graphical checkboxes.
D
The code change made no difference.
Without the graphical checkboxes (enhanceCheckboxes) script both the checkAll, CheckDefault scripts work fine. With the graphical checkboxes, the enhanceCheckboxes script works but the buttons for checkAll, CheckDefault don't make any difference to the graphical checkboxes.
The graphical checkboxes script converts the normal checkbox code in order to show the graphical checkboxes to the user.
The checkAll and CheckDefault scripts are working but they don't affect the graphical checkboxes, but as this cannot be seen by the user it makes little. What I need is enhancement to the checkAll and CheckDefault scripts to allow for the graphical checkboxes.
D
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi daimo1, Maybe we crossed posts. See both of my posts, to get the text and graphics right. Peace and joy. mvan
ASKER
Thanks mvan
Have got the default bit workng now with a bit of tinkering with your suggestion:
document.Form1.Box1.checke d = true;
document.Form1.Box1.previo usSibling. src="image s/checked. gif";
The toggle code works for selecting all but not deselecting all:
function checkAll(f,check){
for(i=0;i<f.elements.lengt h;i++)
if (f.elements[i].type=='chec kbox')
if (f.elements[i].checked!=ch eck) {
f.elements[i].checked=chec k;
f.elements[i].previousSibl ing.title= "Checked";
f.elements[i].previousSibl ing.src="i mages/chec ked.gif";
Have got the default bit workng now with a bit of tinkering with your suggestion:
document.Form1.Box1.checke
document.Form1.Box1.previo
The toggle code works for selecting all but not deselecting all:
function checkAll(f,check){
for(i=0;i<f.elements.lengt
if (f.elements[i].type=='chec
if (f.elements[i].checked!=ch
f.elements[i].checked=chec
f.elements[i].previousSibl
f.elements[i].previousSibl
ASKER
No problem, just created separte script:
function uncheckAll(f,check){
for(i=0;i<f.elements.lengt h;i++)
if (f.elements[i].type=='chec kbox')
if (f.elements[i].checked!=ch eck) {
f.elements[i].checked=chec k;
f.elements[i].previousSibl ing.title= "Unchecked ";
f.elements[i].previousSibl ing.src="i mages/unch ecked.gif" ;
function uncheckAll(f,check){
for(i=0;i<f.elements.lengt
if (f.elements[i].type=='chec
if (f.elements[i].checked!=ch
f.elements[i].checked=chec
f.elements[i].previousSibl
f.elements[i].previousSibl
Hi daimo1,
I'm glad I could help.
Just curious, why the 'B' grade? I can see that my help has you aimed in the right direction, and you're now extending the knowledge, as demonstrated by your previous comment. Good for you! That's what Experts Exchange is all about - people helping people increase their knowledge and understanding.
Peace and joy. mvan
I'm glad I could help.
Just curious, why the 'B' grade? I can see that my help has you aimed in the right direction, and you're now extending the knowledge, as demonstrated by your previous comment. Good for you! That's what Experts Exchange is all about - people helping people increase their knowledge and understanding.
Peace and joy. mvan
ASKER
My aplogies mvan, I meant to give you an A grade - I usually do when people are prompt, ethusiastic and help me out.
Hope I didn't offend.
D
Hope I didn't offend.
D
Hi D,
Thanks for the explanation. We're OK - you and I, that is. (Not me and me, that would be psychological...)
Peace and joy. mvan
Thanks for the explanation. We're OK - you and I, that is. (Not me and me, that would be psychological...)
Peace and joy. mvan
Try this:
function checkAll(f,check){
for(i=0;i<f.elements.lengt
if (f.elements[i].type=='chec
if (f.elements[i].checked!=ch
}
By Nab