replace text to variable's value using jquery

Multi tool use
replace text to variable's value using jquery
I have created a function to get sms message
from db using ajax get call
and after success i want to replace the {order_id}
text to order-id like 454574
and may b few more variables like customer name etc.
sms message
ajax get call
{order_id}
454574
MY TRY
$(document).delegate('.sms-template', 'click', function() {
var tempID = $(this).attr('id').replace('sms-template-','');
var oID = $(this).attr('data-id').replace('sms-template-','');
$.ajax({
url: 'index.php?route=sale/order/getSmsTemplate&token=<?php echo $token; ?>&template_id='+tempID,
type: 'get',
dataType: 'json',
success: function(json) {
$('textarea#input-order-comment-'+oID).append(json['message']);
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "rn" + xhr.statusText + "rn" + xhr.responseText);
}
});
});
It works fine and display message
in textarea
as
message
textarea
Dear {customer_name}, Your Order : {order_id} is successfully placed and ready to process.
i want to replace {customer_name} & {order_id} with its number which i have stored in php variable.
PS: I don't have much knowledge of RegEx.
"You order Id: {order_id} is handover to courier.".replace("{order_id}","454574")
i need it dynamically... 1 script for every order
– Moiz Ahmad
Jun 30 at 9:06
Of course, posted an answer
– LGSon
Jun 30 at 9:10
1 Answer
1
The simplest here would most likely be a replace()
replace()
string.replace("old value","new value")
E.g.
var the_id = "454574"; // or where you get that from
var new_message = json['message'].replace("{order_id}",the_id);
$('textarea#input-order-comment-'+oID).append(new_message);
Updated based on a comment and question edit
To replace additional info, one could do something like this
var the_id = "Order id", the_name = "Customer name";
var new_message = json['message'].replace("{order_id}",the_id).replace("{customer_name}", the_name);
$('textarea#input-order-comment-'+oID).append(new_message);
If there will many more replacements, here is a post that have some clever solutions:
what if i want to replace two things, order_id and customer_name ...then?
– Moiz Ahmad
Jun 30 at 9:14
@MoizAhmad As there is no info about those in the question, I really can't say. Maybe if you show how
json
looks like, and assuming those info are in there...?– LGSon
Jun 30 at 9:16
json
if message is like , Dear {customer_name}, Your Order : {order_id} is successfully placed and ready to process.
– Moiz Ahmad
Jun 30 at 9:18
then how can i change with variable using your script?
– Moiz Ahmad
Jun 30 at 9:18
@MoizAhmad Updated my answer with a 2nd sample
– LGSon
Jun 30 at 9:22
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What about a simple replace, e.g.
"You order Id: {order_id} is handover to courier.".replace("{order_id}","454574")
– LGSon
Jun 30 at 9:05