Pass ids as parameter to Function js

Asked

Viewed 861 times

0

I have a function that you would like to receive the id values of some quantity, unit value and result fields. 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>
 <input id="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>

`

  • 1

    You have a onKeyUp in an input readonly... that will never be called.

  • @Sergio took the onkeyup left so: id('campo4').value = String(total/100).formatMoney(); and I took the third parameter of the function but it doesn’t work

2 answers

1

You have a function receiving 3 parameters, but you are always passing only one which is the id of the clicked instance. In case you would have to fetch the 3 ids within 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();

You need it to be with dynamic ids?

  • yes actually but forward I intend to use the code for a table in which I will have several fields that will come from a database search so I won’t know for sure the name of each id because the amount of items returned by the search is variable. Thanks for the tip! really good, but I really need the ids without having to define them :/

0


Thank you to everyone who helped, but I managed to do so:

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>
<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>

<p>INPUTS DE QUANTIDADE:</p>
<input id="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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.