Reputation: 582
I need to append string something like this {"Key": "Value"}
is got from API response which needs to be added to HTML data-attribute.
Problem: HTML parser changes single quote string to double quotes and ignore rest of the string. Display rest of the string in the HTML attribute color.
var baseDiv = $('#base');
var action = '{"key1": "Let's give a value to it "}';
var targetDiv = "<a href="javascript:void(0);" data-action=\'' + JSON.stringify(action) + '\'></a>"
baseDiv.append(targetDiv);
Upvotes: 0
Views: 789
Reputation: 2335
var baseDiv = $('#base');
var action = '{"key1": "Let\'s give a value to it "}';
var targetDiv = '<a href="javascript:void(0);" data-action="' + JSON.stringify(action) + '"></a>'
baseDiv.append(targetDiv);
And with JQuery this is better:
var baseDiv = $('#base');
var action = '{"key1": "Let\'s give a value to it "}';
var targetDiv = $('<a href="javascript:void(0);">asdasd</a>');
targetDiv.attr('data-action', action);
baseDiv.append(targetDiv);
Upvotes: 1
Reputation: 15351
This has nothing to do with HTML5, you are a little lost with string sequences, probably this is what you need:
var targetDiv = '<a href="javascript:void(0);" data-action="' + JSON.stringify(action) + '"></a>';
Upvotes: 1