Multiple Checked Checkbox Events
I have several checkboxes in a search results list that I want to call a function when they are clicked. The problem I am having now is that when I click one, it is running the function for each one that has been checked instead of just the current one checked. So it works fine the first checked checkbox but say I have 2 checked. When I click the third one it will run the function 3 times.
- $("#search-results-table :checkbox").click(function(){
countChecked($(this).attr("name"));
});
function countChecked(val) {
var n = $("#search-results-table input:checked").length;
$("#compare-header").text(n + (n <= 1 ? " is" : " are") + " checked!");
var index = $.inArray(val, compArray);
if (index == -1) {
- compArray.push(val);
} else {
compArray.splice(index, 1);
}
var str = "";
for (i = 0; i < compArray.length; i++) {
var newStr = compArray[i].slice(4);
str += newStr;
if (i != compArray.length - 1) str += ",";
}
updateCompare(str);
}