Reputation: 1227
I am storing a PHP array variable in Jquery variable.
Following is the code that I am using:
<script>
var tagger = '<?php echo json_encode($tags); ?>';
var obj = jQuery.parseJSON(tagger);
$.each(obj, function(key,value)
{
$("#post_tags").tagging("add", value);
});
</script>
In tagger variable, I am getting this below data.
var tagger = '["sdf"," da"," adf"," ad"]';
But when I am running the loop it is showing values from the second index, the first index value is being eliminated.
Only these values are visible in the field : '[" da"," adf"," ad"]'
.
The sdf
value is not displayed.
May I know that where is it being wrong, as far as I am concerned the code is good to go. But still, want to confirm that is there anything missing.
Upvotes: 0
Views: 52
Reputation: 549
You don't have to parse the tagger because it already in json format:
<script>
var tagger = <?php echo json_encode((array)$tags);?>;
$.each(tagger, function(key,value)
{
$("#post_tags").tagging("add", value);
});
</script>
Upvotes: 0
Reputation: 208
<script>
// try this
var obj = <?php echo json_encode($tags); ?>;
//var obj = jQuery.parseJSON(tagger);
$.each(obj, function(key,value)
{
$("#post_tags").tagging("add", value);
});
</script>
Upvotes: 1