I need to get the value id="valorPorcentagem"
which is the value of the percentage with the (id="valor_vendedor")
and automatically add in the id="comissao"
.
Being that:
valorPorcentagem / 100 x valor_vendedor = comissao
20 / 100 x 800,00 = 160,00
45 / 100 x 700,00 = 315,00
The total amount of the commission = R $ 475,00
html
<scriptsrc="https://code.jquery.com/jquery-3.1.1.js"></script>
<b>Comissão do Vendedor:</b>
<input type="text" name="total" id="comissao" readonly>
<br><br>
<select id="valorPorcentagem">
<option value="20">20</option>
<option value="45">45</option>
</select>
<input id="valor_vendedor" onkeyup="calcular()">
javascript
function calcular() {
var soma = $('.calcular').get().reduce(function(soma, el) {
return (parseFloat(el.value.replace(/\./g, "").replace(",", "."), 10) || 0) + soma;
}, 0);
soma = soma.toFixed(2);
document.getElementById('comissao').value = soma;
mascara(document.getElementById('comissao'), mreais);
}
The (Add Plan) button in HTML
<input type="text" name="usuarios[0][comissao_vendedor]" id="valor_vendedor" class="calcular form-control" placeholder="R$" onkeypress="mascara(this,mreais)" onkeyup="calcular()">
$ js
var AddTableRow = function(el) {
var tbody = $(el).closest('table').find('tbody');
var row = tbody.find('tr:last').clone();
var name = row.find('.calcular').attr('name');
var index = parseInt(name.match(/informacao\[(\d+)\]\[porcentagem\]/)[1], 10) + 1;
row.find('[name^="informacao["]').each(function() {
if (this.name) {
this.name = this.name.replace(/^informacao\[\d+\]/, "informacao[" + index + "]");
}
});
tbody.append(row);
};