anson920520 anson920520 - 5 months ago 15
HTML Question

apple .replace() Html element generate by handlebar js

I am wondering if how am i able to change the element data by

if i use handlebar js to generate html elements.

For instance i have this role of p tag which display a row of data by handlebar js:

<p id="pre-region">{{region}}</p>

and the result of it is


and i 'd like to change it to


If you haven't had any experience of handlebar js then consider the tag be

<p id="pre-region">1,44</p>

how should i change from 1,44 to 1 +44?


Here should be an extersion for my question. I am passing the HTML element inside pre-region into an href in order to update my website by Ajax.

After i have converted all the comma in to "+" the API retrieve special character "&B2" which equal to the symbol "+" and the API goes error.


This is how may API looks like at the moment


should be the solution


I haven't had any experience of handlebars.js but from my point of view, you can just put the code just before the </body>:

  var node = document.getElementById('pre-region');
  node.innerHTML = node.innerHTML.replace(',', '+');

I'll check out the handlebars js in case it does not work.

Update: As you mentioned in the comment, if you need to use it in the HTTP request/URL, you may handle the string using decodeURIComponent(yourstring):

decodeURIComponent('1%2B44'); // you get '1+44'

Read more about decodeURIComponent() method from this. In URL, it should be encoded as region=1%2B44 in your case; while it should be decoded if you want to use it in your JavaScript code or display in the web page.

Update 1

You should encode your string when it's used as a part of parameter of HTTP request. Therefore, it looks good if the URL is:


What you need to do is decode the string on your server side. I assume that you are in control of the server side. If you are using Node.js, you can just use decodeURIComponent() method to decode the parameter. If you're using Python or PHP as your server language, it should be something like decodeURIComponent() in that language.