I've muddled some JS code together which appears to work in firefox (no errors and functions correctly) but throws up 'SyntaxError: Unexpected token '='. Expected a ')' or a ',' after a parameter declaration.' in safari. I have the following object in php
$items = Array ( [0] => stdClass Object ( [id] => 1 [class] => class_a [make] => Kia [model] => Picanto [features] => 3,4,5,6,7,8 [colour] => white [engine] => 1000cc [ordering] => 1 [published] => 1 [image] => images/vehicles/picanto.jpg ) [1] => stdClass Object ( [id] => 2 [class] => motorbike [make] => Honda [model] => Transalp [features] => [colour] => blue [engine] => 650cc [ordering] => 6 [published] => 1 [image] => ) [2] => stdClass Object ( [id] => 3 [class] => moped [make] => Sym [model] => SR [features] => [colour] => white [engine] => 150cc [ordering] => 5 [published] => 1 [image] => ) [3] => stdClass Object ( [id] => 4 [class] => class_b [make] => Suzuki [model] => Splash [features] => 12 [colour] => Red [engine] => 1300cc [ordering] => 3 [published] => 1 [image] => images/vehicles/suzuki_splash.jpg ) [4] => stdClass Object ( [id] => 5 [class] => class_f [make] => Peugot [model] => 307 Cabrio [features] => 8,9,10,11 [colour] => Black [engine] => 1600cc [ordering] => 4 [published] => 1 [image] => images/vehicles/peugeot307.jpg ) [5] => stdClass Object ( [id] => 6 [class] => class_a [make] => Hyundai [model] => Atos [features] => [colour] => white [engine] => 1100cc [ordering] => 2 [published] => 1 [image] => images/vehicles/atos.jpg ) )
And the following javascript
<script type="text/javascript">
jQuery(document).ready(function($){
var items = <?PHP echo json_encode($items)?>;
console.log(items);
for (var i = 0, len = items.length; i < len; i++) {
lookup[items[i].id] = items[i]; //access the new lookup object using lookup[id].variable i.e lookup[1].image
}
var vId ="";
function updateImg(img, display=true){
if (display == true){
$("#vehicle-image").show(500);
}else{
$("#vehicle-image").hide(500);
}
$("#vehicle-image").attr("src", '/'+ img);
}
$('#jform_vehicle').on('change', function() {
var vId = parseInt(($("#jform_vehicle").chosen().val()));
console.log (vId);
if (isNaN(vId) !==true){
var img = lookup[vId].image;
console.log ('img=' + img);
updateImg(img);
}else{
console.log('not a number');
var img = "";
updateImg(img, false)
}
});
});
</script>
in the browser the json_encode line is as follows
var items = [{"id":"1","class":"class_a","make":"Kia","model":"Picanto","features":"3,4,5,6,7,8","colour":"white","engine":"1000cc","ordering":"1","published":"1","image":"images\/vehicles\/picanto.jpg"},{"id":"2","class":"motorbike","make":"Honda","model":"Transalp","features":"","colour":"blue","engine":"650cc","ordering":"6","published":"1","image":""},{"id":"3","class":"moped","make":"Sym","model":"SR","features":"","colour":"white","engine":"150cc","ordering":"5","published":"1","image":""},{"id":"4","class":"class_b","make":"Suzuki","model":"Splash","features":"12","colour":"Red","engine":"1300cc","ordering":"3","published":"1","image":"images\/vehicles\/suzuki_splash.jpg"},{"id":"5","class":"class_f","make":"Peugot","model":"307 Cabrio","features":"8,9,10,11","colour":"Black","engine":"1600cc","ordering":"4","published":"1","image":"images\/vehicles\/peugeot307.jpg"},{"id":"6","class":"class_a","make":"Hyundai","model":"Atos","features":"","colour":"white","engine":"1100cc","ordering":"2","published":"1","image":"images\/vehicles\/atos.jpg"}];
Which looks right so I don't know what is causing the error. Any ideas? Cheers.
Safari points to an error here in the code safari error