So this is a message inbox style page for a site Im making. I've used checkbox arrays before and always found them to be really difficult to make work right.
This one just puts a checkbox for each message, and one master one at the top (not in the array) to enable check/uncheck all.
There's then a load of Javascript functions which push AJAX requests out to php which deletes messages or whatever.
I've finally got back round to looking at it today. When I click the checkbox that should tick them all, only the first and 3rd are ticked, the second remains clear.
All the Javascript is is a loop that goes through the checkboxes calling the .click() method (As there is added functionality that is run when each of these checkboxes is clicked, too).
For some reason, the length of the array is reported as 5 (3 checkboxes = 5 array positions?!) and both array pos 1 and 2 seem to affect the second checkbox - First ticking, then unticking it as it cycles through.
I really cannot fathom how one checkbox can occupy two positions....
There's a boat load of code below, Javascript for the function, and the HTML output of the php script I'm using to generate the page.
Code:
function checkAll(obj){
var checkBoxes=new Array();
checkBoxes = document.msgBox.checkArr;
//alert(checkBoxes.length);
for (var i=0;i<(checkBoxes.length-1);i++){
checkBoxes[i].click();
//alert(i);
}
}
Re: Problem utilizing checkbox array with Javascript
As the cat said, it's better to group checkboxes by name and use getElementsByName().
Wanna install linux on a vacuum cleaner. Could anyone tell me which distro sucks better?
I had a nightmare last night. I was dreaming that I’m 64-bit and my blanket is 32-bit and I couldn’t cover myself with it, so I’ve spent the whole night freezing. And in the morning I find that my blanket just had fallen off the bed. =S(from: bash.org.ru)
//always looking for job opportunities in AU/NZ/US/CA/Europe :P
willCodeForFood(Arrays.asList("Java","PHP","C++","bash","Assembler","XML","XHTML","CSS","JS","PL/SQL"); USE [code] TAGS!Read this FAQ if you are new here. If this post was helpful, please rate it!
Bookmarks