Pass ids as parameter to function js

0

I have function that I would like to receive the id values of some fields of quantity, unit value, and result. But I'm not getting it.

'

String.prototype.formatMoney = function() {
    var v = this;

    if(v.indexOf('.') === -1) {
        v = v.replace(/([\d]+)/, "$1,00");
    }

    v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
    v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");

    return v;
};

function id( el ){
    return document.getElementById( el );
}

function getMoney( el ){
    var money = id( el ).value ? id( el ).value.replace( ',', '.' ) : 0;
    return parseFloat( money )*100;
}

function multiplicacao(id1, id2, id3){
    alert(id1, id2, id3);
    var qtd = id(id1).value;
    var unit = getMoney(id2);
    var total = qtd*unit;
    id(id3).value = String(total/100).formatMoney();
}

//mascara para campos de R$
function moeda(z){          
v = z.value;        
v=v.replace(/\D/g,"")  
//permite digitar apenas números    
v=v.replace(/[0-9]{12}/,"inválido")   
//limita pra máximo 999.999.999,99  
v=v.replace(/(\d{1})(\d{8})$/,"$1.$2")  
//coloca ponto antes dos últimos 8 digitos  
v=v.replace(/(\d{1})(\d{5})$/,"$1.$2") 
//coloca ponto antes dos últimos 5 digitos  
v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2")    
//coloca virgula antes dos últimos 2 digitos        
z.value = v;    }
<html>
<head>
<script type="text/javascript" src="js/funcoes.js"></script>  
<script type="text/javascript" src="js/jquery-1.11.2.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script></head><body><inputid="campo1" onKeyUp='multiplicacao(this.id)' type='text' name='' class='form-control' size='10'/>

<input id="campo2" onKeyUp='moeda(this);multiplicacao(this.id)' type='text' name='' class='form-control' size='10'/>

<input id="campo4" onKeyUp='moeda(this);multiplicacao(this.id)' name="campo4" readonly="readonly"  class='form-control' size='10'/>
</body>
</html>

'

    
asked by anonymous 21.06.2017 / 16:54

2 answers

0

Thanks to everyone who helped, but I did it like this:

i = 1;

String.prototype.formatMoney = function() {
    var v = this;

    if(v.indexOf('.') === -1) {
        v = v.replace(/([\d]+)/, "$1,00");
    }

    v = v.replace(/([\d]+)\.([\d]{1})$/, "$1,$20");
    v = v.replace(/([\d]+)\.([\d]{2})$/, "$1,$2");

    return v;
};

function id( el ){
    return document.getElementById( el );
}

function getMoney( el ){
    var money = id( el ).value ? id( el ).value.replace( ',', '.' ) : 0;
    return parseFloat( money )*100;
}

function multiplicacao(){

    //pego os elementos pela classe
    var inputqtd = $('.input-qtd');
    var inputunit = $('.input-unit');
    var inputresultado = $('.input-resultado');
    var qtd = 0; var unit = 0; var total = 0; 

    //varro a lista e pego os ids.
    for (var i = 0; i < inputqtd.length; i++) {
        id1 = (inputqtd[i].id);
        id2 = (inputunit[i].id);
        res = (inputresultado[i].id);

        qtd = id(id1).value;
        unit = getMoney(id2);
        total = qtd*unit;
        id(res).value = String(total/100).formatMoney();
    }

}

//mascara para campos de R$
function moeda(z){          
    v = z.value;        
    v=v.replace(/\D/g,"")  
//permite digitar apenas números    
v=v.replace(/[0-9]{12}/,"inválido")   
//limita pra máximo 999.999.999,99  
v=v.replace(/(\d{1})(\d{8})$/,"$1.$2")  
//coloca ponto antes dos últimos 8 digitos  
v=v.replace(/(\d{1})(\d{5})$/,"$1.$2") 
//coloca ponto antes dos últimos 5 digitos  
v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2")    
//coloca virgula antes dos últimos 2 digitos        
z.value = v;    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><html><head><scripttype="text/javascript" src="js/jquery-1.11.2.min.js"></script>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.js"></script></head><body><p>INPUTSDEQUANTIDADE:</p><inputid="input-qtd1" onKeyUp='multiplicacao()' type='text' name='' class='form-control input-qtd' size='10'/>

<input id="input-qtd2" onKeyUp='multiplicacao()' type='text' name='' class='form-control input-qtd' size='10'/>

<input id="input-qtd3" onKeyUp='multiplicacao()' type='text' name='' class='form-control input-qtd' size='10'/>

<input id="input-qtd4" onKeyUp='multiplicacao()' type='text' name='' class='form-control input-qtd' size='10'/>

<p>INPUTS DE VALOR UNITÁRIO:</p>
<input id="input-unit1" onKeyUp='moeda(this);multiplicacao()' type='text' name='' class='form-control input-unit' size='10'/>

<input id="input-unit2" onKeyUp='moeda(this);multiplicacao()' type='text' name='' class='form-control input-unit' size='10'/>

<input id="input-unit3" onKeyUp='moeda(this);multiplicacao()' type='text' name='' class='form-control input-unit' size='10'/>

<input id="input-unit4" onKeyUp='moeda(this);multiplicacao()' type='text' name='' class='form-control input-unit' size='10'/>

<p>INPUTS DE TOTAL:</p>
<input id="input-resultado1" onKeyUp='moeda(this);multiplicacao()' name="campo4" readonly="readonly"  class='form-control input-resultado' size='10'/>

<input id="input-resultado2" onKeyUp='moeda(this);multiplicacao()' name="campo4" readonly="readonly"  class='form-control input-resultado' size='10'/>

<input id="input-resultado3" onKeyUp='moeda(this);multiplicacao()' name="campo4" readonly="readonly"  class='form-control input-resultado' size='10'/>

<input id="input-resultado4" onKeyUp='moeda(this);multiplicacao()' name="campo4" readonly="readonly"  class='form-control input-resultado' size='10'/>

</body>
</html>
    
22.06.2017 / 03:34
1

You have a function that receives 3 parameters, but is always passing only one that is the id of the clicked instance. In case you would have to get the 3 ids inside the function, one way is to leave the 3 ids predefined.

function multiplicacao(){
  var ids = ['campo1', 'campo2', 'campo4'];
}
var qtd = id(ids[0]).value;
var unit = getMoney(ids[1]);
var total = qtd*unit;
id(ids[2]).value = String(total/100).formatMoney();

Do you need it to be with dynamic ids?

    
21.06.2017 / 21:51