I am working on a Google Chrome extension which hides, moves, and otherwise alters elements of the current page, utilizing contentscripts to do so. I am working on a page which has a few elements I change, so I have a storage.sync value set for each. Due to the drastic nature of the changes each option effects, I have the toggles for each in my popup. I have tried to have each checkbox set the storage.sync value for the id of the checkbox to the boolean value of the checkbox using jQuery, specifically jQuery.click and jQuery.each. My popup's html is:
<html>
<head>
<title>Options</title>
<script src="jquery-3.1.1.min.js"></script>
<script src="popup.js"></script>
</head>
<body style="width: 400px">
<h3>Settings</h3>
<input class="option" id="opt1" type="checkbox">Option 1.</input><br>
<input class="option" id="opt2" type="checkbox">Option 2.</input><br>
<input class="option" id="opt3" type="checkbox">Option 3.</input>
<button id="other">TBI</button>
</body>
</html>
where 'jquery-3.1.1.min' is the minified jQuery source and popup.js is:
$(document).ready(function() {
$('.option').each(function(index, elem) {
chrome.storage.sync.get(elem.id, function (value) {
elem.checked = value.fix;
});
elem.change(function() {
chrome.storage.sync.set({this.id: this.checked);
});
});
});
I have also tried the replacing this
with everything from elem
to $(this)[0]
, as well as $(elem)
, but each time I get errors of a different source. In general, elem
produces Uncaught SyntaxError: Unexpected token .
and this
produces Uncaught SyntaxError: Unexpected token this
.
Why does this occur? How can I avoid this? I am primarily a python coder, so I expect I am misunderstanding the scope of each value or the meaning of this
. What I don't understand is how that translates into Unexpected token
errors. Thank you!