JCHASE11
JCHASE11

Reputation: 3941

String concatenation not working, throwing error

I am storying a dynamic string in a variable and then resuing that string later in the code. It is throwing an error, "Uncaught SyntaxError: Unexpected token +"

var template_url = "<?php bloginfo('template_url'); ?>";

$(document).ready(function() {

    var urlCameraDic = 
          {
            template_url + '/sliders/walking.html' : 'camera_1',
            template_url + '/sliders/lobby.html' : 'camera_2',
            template_url + '/sliders/cafe.html' : 'camera_3',
            template_url + '/sliders/womens.html' : 'camera_4'
          };
});

I am not sure why this error is being thrown. Am I concatenating this incorrectly?

Upvotes: 1

Views: 824

Answers (2)

Jacob T. Nielsen
Jacob T. Nielsen

Reputation: 2978

You cannot execute javascript when declaring the keys of an object by object literal.

You can do this though

var urlCameraDic = {};
urlCameraDic[template_url + '/sliders/walking.html'] = 'camera_1';
urlCameraDic[template_url + '/sliders/lobby.html'] = 'camera_2';
...

Upvotes: 5

Benjamin Kaiser
Benjamin Kaiser

Reputation: 2227

You need to define your custom keys after creation like so:

var urlCameraDic = {};
urlCameraDic[template_url + '/sliders/walking.html'] = 'camera_1';
// add the other items like above

See this similar SO answer for more details.

Upvotes: 1

Related Questions