Korak
Korak

Reputation: 251

Function treats string as single value instead of parsing

I'm using a tags plugin that takes input like this just fine:

$(this).tagHandler({
   assignedTags: [ 'test','from','reddit' ]
});

If however I create a variable named tags that is a string:

tags = "'test','from','reddit'";

And attempt to use it in the function it gets treated as a single string.

$(this).tagHandler({
   assignedTags: [ tags ]
});

Instead of being processed through the function I end up with 'test','from','reddit' as a single tag.

I have a feeling this is a common problem but haven't found the right search phrase to identify the solution.

Upvotes: 0

Views: 81

Answers (4)

easel
easel

Reputation: 4048

I'm not sure why you expect the string to be parsed, does the plugin indicate it will do that? My guess is the following might work:

$(this).tagHandler({
   assignedTags: [ tags.split(',') ]
});`

Upvotes: 0

Alnitak
Alnitak

Reputation: 339816

You need to supply an array - the string you've created won't automatically get converted into one:

var tags = ['test', 'from', 'reddit'];
$(this).tagHandler({
   assignedTags: tags
});

If all you have is a string, try:

var str = 'test,from,reddit';
var tags = str.split(',');
$(this).tagHandler({
   assignedTags: tags
});

Upvotes: 2

mcgrailm
mcgrailm

Reputation: 17640

that's because this is

 [ 'test','from','reddit' ]

is an array

and what you have does in put a sting into an array you should

what you want is to make an array

 var tags = ['test','from','reddit'];

then add it

$(this).tagHandler({
    assignedTags: tags 
 });

Upvotes: 0

Calum
Calum

Reputation: 5316

Using an array may work.

tags = new Array('test','from','reddit');
$(this).tagHandler({
   assignedTags: tags
});

Upvotes: 0

Related Questions