Currently I have a working block of code for a Chrome Extension, however I have a variety of code blocks which repeat themselves and I am looking to minimise the code required.
document.getElementById('streamSelect').addEventListener('change', function() {
var t = document.getElementById("streamSelect").value;
chrome.storage.sync.set({ "streamSelect" : t }, function() {});
}, false)
document.getElementById('volumeSlider').addEventListener('change', function() {
var u = document.getElementById("volumeSlider").value;
chrome.storage.sync.set({ "volumeSlider" : u }, function() {});
}, false)
document.getElementById('currentSegment').addEventListener('change', function() {
var v = document.getElementById("currentSegment").value;
chrome.storage.sync.set({ "currentSegment" : v }, function() {});
}, false)
document.getElementById('nowPlaying').addEventListener('change', function() {
var w = document.getElementById("nowPlaying").value;
chrome.storage.sync.set({ "nowPlaying" : w }, function() {});
}, false)
document.getElementById('lastPlayed').addEventListener('change', function() {
var x = document.getElementById("lastPlayed").value;
chrome.storage.sync.set({ "lastPlayed" : x }, function() {});
}, false)
document.getElementById('quickLinks').addEventListener('change', function() {
var y = document.getElementById("quickLinks").value;
chrome.storage.sync.set({ "quickLinks" : y }, function() {});
}, false)
document.getElementById('desktopNotifications').addEventListener('change', function() {
var z = document.getElementById("desktopNotifications").value;
chrome.storage.sync.set({ "desktopNotifications" : z }, function() {});
}, false)
As a result I have tried to create a smaller code block that listens for a change to any HTML Select element. It is successfully activating on any change done to a Select element and it is also setting the Y and Z variables correctly, however it does not appear to be completing the execution of the Chrome.Storage.Sync.Set line.
var setOptions = ["streamSelect", "volumeSlider", "currentSegment", "nowPlaying", "lastPlayed", "quickLinks", "desktopNotifications"]
$("select").change(function() {
for(var i = 0; i < setOptions.length; i++) {
var Y = setOptions[i];
var Z = document.getElementById(Y).value;
chrome.storage.sync.set({ Y : Z }, function() {});
}
})
I'm unsure if I am missing something simple for this but any assistance to get this working would be much appreciated at this time.