Home > other >  Javascript function inside Django Loop runs only once
Javascript function inside Django Loop runs only once

Time:02-03

In my template, i generate each table row within a django for loop, I have this function that formats strings the way I want, but it's working only in the first row of the table, I dont know why.

enter image description here enter image description here

CodePudding user response:

Well the problem is basically that in HTML you can't have to elements with the same id, and you are calling them cpfpg then is logic that the first row is ok because you have only one element with that id but after that you have more than one element with the same id and that's the problem.

I think you can change it id="cpfpj{{forloop.counter}}" then you can do in your javascript:

ele = document.getElementById("cpfpj{{forloop.counter}}")

It just fix your problem.

  •  Tags:  
  • Related