-3

I want to use a javascript variable with .attr() value

My code :

var nombre_pv = 18;
var nombre_pv_max = 20; 
var pv_enlever = nombre_pv_max - nombre_pv ;
var pv = nombre_pv_max - pv_enlever;
var pv = pv/nombre_pv_max;
var pv = pv * 100;

$(".hp").attr("value", "/* insert pv here*/");  

1 Answers1

0
$(".hp").val(pv);

Or

$(".hp").prop("value", pv);

You might want to check out this answer to explain why the value attribute on an element is not the same as the property: https://stackoverflow.com/a/5876747/779323

Community
  • 1
  • 1
i_like_robots
  • 2,760
  • 2
  • 19
  • 23