I have a function that gets all the tabs in a window and then updates the popup.html page by adding a checkbox for each tab. After doing this there is a second for loop that adds event listeners which update an dictionary containing the value of the checkbox's. The problem is that any checkbox I click only updates the last element in the dictionary.
Here is the code:
function viewTabs() {
var queryInfo = {lastFocusedWindow: true};
// Go through all the tabs open, and add them to the scrollview along with a number and checkbox
chrome.tabs.query(queryInfo, function(tabs) {
document.getElementById("openLinks").innerHTML = "";
(function() {
for (i = 0; i < tabs.length; i++) {
// add checkboxes to each link for the user to include or disclude
document.getElementById("openLinks").innerHTML +=
"<li><input type=\"checkbox\" id = \"" + i.toString() + "\" checked>" +
tabs[i].title + "</li>";
checkedTabsArr[i.toString()] = true;
}
for (i = 0; i < tabs.length; i++) {
document.getElementById(i.toString()).addEventListener("click",
function() {
console.log(checkedTabsArr);
checkedTabsArr[i.toString()] = !checkedTabsArr[i.toString()];
});
}
}());
});
}
Here is the output (I am clicking different checkboxes not just 6):
Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: false}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: false}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: false}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: true}
Buttons.js:174 Object {0: true, 1: true, 2: true, 3: true, 4: true, 5: true, 6: true, 7: false}
I have tried with and without closures.
Why are you using two separate for loops inside the same context?
EDIT I've tracked this down to an issue with how javascript handles both click and change events with checkboxes.
Note: Don't ever use click events for checkboxes, use
onChange
instead. Also, don't assignonchange
using javascript. It fails spectacularly for some reason that I can't identify.Instead, use jQuery for it. Make sure you have the jQuery library included on your page and load your script after all DOM elements are loaded on the page. Then this code should work for you just fine:
The above code should hopefully be a plug and play solution for you.
Let me know how this goes for you.
Fiddle: https://jsfiddle.net/wof8ebq7/27/