In my php page, i'm including 2 .js files, and the first one creates and populates a js array which I need to access in the second .js file. For some reason, I'm getting an error that the array is undefined when I know for a fact that it's being populated. Why is this broken?
What am I doing wrong?
Error: ReferenceError: sl is not defined
first.js
var sl = [];
for (var i = 0; i < j.length; i++) {
options += '<option value="' + j[i].value + '">' + j[i].text + '</option>';
sl[j[i].value] = [];
sl[j[i].value]['shopping_list'] = j[i].text;
sl[j[i].value]['is_private']= j[i].is_private;
console.log('printing SL for ' + j[i].value);
console.log(sl[j[i].value]['shopping_list']);
}
second.js
$("#shopping_list_name").val(sl[$("#existing_shopping_lists").val()]['shopping_list']);
Is the code that you cited from first.js inside a function (possibly a $(function() {});
)? JavaScript has functional scoping making the var sl=[]
not global (regardless of order)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.