0
I have a function that inserts inputs dynamically in a div enabling the creation of a shopping list, tasks, or what the imagination allows, this also has a button that removes the item you no longer want, type a TODO, I would like that when running the COUNT function it not only counts the inputs but also calculates how many numbers there are in all inputs, example, has 4 inputs so it will calculate 4 inputs and also sum the value within all 4, I already know how to count, I just don’t know how to add.
The input input code is this
function new_input() {
$('<x>'
+ '<input class="cod" placeholder="Código" />'
+ '<input class="desc" placeholder="Descrição" />'
+ '<input class="quant" placeholder="Quantidade" />'
+ '<input class="val" placeholder="Valor" />'
/* este span serve para apagar um item */
+ '<span class="remove cursor_pointer display_none">+</span>'
+ '</x>').prependTo(scnDiv);
$('input').first().focus();
rscnDiv++;
/* este trecho abaixo irá contar um novo input */
contar_mais('itens_total'); } });
For everything to work perfectly, new_input has to be executed inside this function:
function prep_new_input() {
$(this).blur();
event.preventDefault();
$('input').first().focus();
new_input(); }
the function that counts the inputs is this section below
function contar(i) {
return document.getElementById(i); }
function contar_menos(i) {
var quantidade = parseInt(contar(i).value);
if(quantidade > 0)
contar(i).value = quantidade - 1; }
function contar_mais(i) {
contar(i).value = parseInt(contar(i).value) + 1; }
the function that removes an item from the list is the excerpt below
$('span.remove').live('click', function() {
if(rscnDiv > 1) {
$(this).parents('x').remove();
$('input').first().focus();
rscnDiv--;
contar_menos('itens_total'); } });
editing
For everything to work, you need to be in it
$(Document). ready(Function() { var scnDiv = $('div.items'); var rscnDiv = $('div.items x'). size() + 1; new_input();
editing
Divs that handle inputs and show how many inputs they have are as below
<div class='itens'></div>
<input id='itens_total' class='itens_total' value='0' readonly='readonly' />
I wish I could count the amount of input as it already does and also add the values within all inputs in a total quantity div next to the total items, only this is enough, but if break can make it work without only ID with class, Valew, already, grateful
to send an input in case I put?
– flourigh
added to the reply...
– andrepaulo
is, in case, he is not adding up alone and not counting how many inputs have, would have to count the amount of input and add the values entered in, count is already doing in my example, just need to add up alone every time you change the value of the quantity, he is already doing this? in case I would have to put in my code to appear?
– flourigh
Yes... I put this code as an example for you to see how it works, not to make it work for you, you know? is to take the idea I gave you and put it in your code.
– andrepaulo
Gee, perfect, it worked really well, I put in the Count More and Count Less, then it counts the inputs and adds up the values by creating and removing, very grateful, I understood then how it works, don’t get me wrong, it’s not that I wanted you to do it for me, it’s that I didn’t really know, I’m new to jquery
– flourigh
just one more question, I tried several ways but I got no result, I wanted to add the amount of the sum input when the change event happens or else Blur, nothing is happening $('input.Quant'). on('change', Function() { somar_quantity(); }); does not work even with Blur
– flourigh