1

My template code is as follows

{% for content in contentlist %}
  <div class="card card-margin-top">
      <div class="card-body">
           <div class="card-head">
              <p class="card-title">PRICE&nbsp;&#8208;&nbsp;
                        <span id="price">{{content.price}}</span>
              </p>
           </div>
      </div
  </div>           
{% endfor %}

and I want to convert my price value into a comma-separated value using the js function. JS function is as follows

window.onload=function(){
  var price = document.getElementById("reservedPrice").innerText
  x = updateTextInput(price)
  document.getElementById("reservedPrice").innerText = x
}

function updateTextInput(val) {
  x = val.toString();
  var lastThree = x.substring(x.length-3);
  var otherNumbers = x.substring(0,x.length-3);
  if(otherNumbers != '')
      lastThree = ',' + lastThree;
  var res = otherNumbers.replace(/\B(?=(\d{2})+(?!\d))/g, ",") + lastThree;
  return res
}

but in result looks like this. JS executing only on first entry. I want all prices to be comma-separated.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    I guess you are trying to do comma separation for number values, [this](https://stackoverflow.com/a/347560/8601641) might help. – k33da_the_bug Feb 19 '21 at 05:38
  • Thanks for the suggetion but I want to use comma separated values according to indian currency like 2,00,00,000 not 200,000,000 – Akshay Sanas Feb 19 '21 at 06:17
  • @AkshaySanas The problem is you set the id of an element in a loop. An elements id should be unique, when you write any code to change the element with an id only the first one would be selected. You want to use a class instead. – Abdul Aziz Barkat Feb 19 '21 at 06:23
  • There is third party package similar to intcomma filter for Indian format see [this](https://github.com/narenchoudhary/django-indian-numbers). – k33da_the_bug Feb 19 '21 at 06:27
  • @AbdulAzizBarkat I tried with class as well..can you tell me how can I use it please? – Akshay Sanas Feb 19 '21 at 06:40
  • 1
    Thanks @k33da_the_bug the link works and working as expected..you saved my time – Akshay Sanas Feb 19 '21 at 06:53

0 Answers0