Home Custom input number in a foreach loop

# Custom input number in a foreach loop

Marco Romano
1#
Marco Romano Published in 2018-02-14 08:48:31Z
 I have make a new style for my input number, and this work well. My problem is when I add this is a foreach structure this doesn't work. I think the problem is the ID change the value, but inside the foreach all all inputs have the same ID. This is my code inside the foreach:   This is the js code:  function increaseValue() { var value = parseInt(document.getElementById('number').value, 10); value = isNaN(value) ? 0 : value; value++; document.getElementById('number').value = value; } function decreaseValue() { var value = parseInt(document.getElementById('number').value, 10); value = isNaN(value) ? 0 : value; value < 1 ? value = 1 : ''; value--; document.getElementById('number').value = value; }  I have try to give unique id, but dosen't work. If I add like id ?> instead of id number this does not work. How I can do? If needed, I can also change the html of the input. Thank you This is My last test: 
 I try to add the js inside the foreach, with the same id of the input. But doesn't work.
Nina Scholz
2#
Nina Scholz Reply to 2018-02-14 09:25:31Z
 You could use the id of the input as parameter for calling increaseValue and decreaseValue. The attribute id should be unique and you could use a proper prefix and the product number/identifier for it. Maybe you change the quoting for attribute to ", instead of ' (this could be a matter of a given style guide). If you do so, then stay allways by the same style. You could nest quotes by using the other unused quote inside of a javascript parameter. onclick="decreaseValue('product[id ?>]')" ^ ^ outer ^ ^ inner  function increaseValue(id) { var value = +document.getElementById(id).value || 0; value++; document.getElementById(id).value = value; } function decreaseValue(id) { var value = +document.getElementById(id).value || 0; value--; if (value < 1) { value = ''; } document.getElementById(id).value = value; }  
 You need to login account before you can post.
Processed in 0.696913 second(s) , Gzip On .