Exchanging values between functions with Javascript

Asked

Viewed 171 times

0

Hello, in my code how do I call the function (#print) the values contained in the "data" array are obtained from the generating functionString.

var gerarString = function () {
    var codproduto = 001,
    var newRow = 'teste',
    var iten = ([codproduto,newRow]);

    var dados = new Array();
    dados.push(iten);

    return dados;
}

$("#imprimir").on('click', function(){
    console.log(gerarString.dados);
 //$("#tabvenda").prepend(gerarString.getDados());
});

  • 1

    I don’t understand your problem or how you intend to use this array within an array. console.log(gerarString()); seems like the solution, but I think the real problem you have is another and it would be interesting to see it so that I can understand and help better.

  • @Sergio looking at all the code will be easier to understand. What I intend to do is that when selecting some checkbox, the data (value) will be stored in an array and sent to table 1 by clicking submit. The intention of using an array within another array is that it would use product code to validate if the same checkbox is clicked more than once, preventing it from being duplicated. https://jsfiddle.net/LGian/4Lpu7vao/3/

  • I believe that there must be a more "clean" way to do this process, but I still do not know.

1 answer

1


There are some problems with your code... The code below shows on the console the output:

1,test

<!DOCTYPE html>
<html>
<head>
  <script   src="https://code.jquery.com/jquery-3.1.0.min.js"   integrity="sha256-cCueBR6CsyA4/9szpPfrX3s49M9vUU5BgtiJj06wt/s="   crossorigin="anonymous"></script>
</head>
<body>

<button type="button" id="imprimir">Click Me!</button>

</body>
<script>
function gerarString() {
    var codproduto = 001;
    var newRow = 'teste';
    var iten = ([codproduto,newRow]);

    var dados = new Array();
    dados.push(iten);

    return dados;
}

$("#imprimir").on('click', function(){
    console.log(gerarString().toString());
 //$("#tabvenda").prepend(gerarString.getDados());
});
</script>
</html>
  • Using toString() pulled the values. Thank you

Browser other questions tagged

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