I have this code:
$(document).on('click','.submitMessage,.submitStdMessage', function(e){
prevContent=$('textarea').val();
alert(prevContent);
variables = {
'{nom}' : 'Manuel',
'{apl}' : 'García',
'{var1}' : 'chips',
'{var2}' : 'deportes y aventura',
'{var3}' : 'informática y tecnología',
'{cst1}' : 'Serrano, 28',
'{cst2}' : 'Plaza del carmen, 32',
'{cst3}' : 'García Luna, 15'
};
$.each(variables, function (key, value) {
newContent = prevContent.replace(key, value);
});
alert(newContent);
});
When a string like this is passed:
{nom}{var2}{cst2}{cst1}{cst3}
First alert says:
{nom}{var2}{cst2}{cst1}{cst3}
Second alert says:
{nom}{var2}{cst2}{cst1}García Luna, 15
If I change the order of elements inside the variables array, it always replaces the variable set on the last position of array. I need it to replace all variables send.