How to insert an array into a text area?

1

How to insert an array into a text area?

var itens = new Array();

var index = 0;
function adicionaItem(item, quantidade) 
{
  itens[index] = {nomeItem : item, quantidade: quantidade};
  index++;
}

function concluirCompra() 
{
   document.getElementById("nota").innerHTML=itens[];//Aqui insere no textarea
}
    
asked by anonymous 07.04.2014 / 00:59

1 answer

3
<html>
<head>
    <title>Texto Area</title>
    <script>
        var itens = new Array();
        var index = 0;  
        function adicionaItem(item, quantidade) 
        {
          itens[index] = {nomeItem : item, quantidade: quantidade};
          index++;
        }

        function concluirCompra() 
        {           
           document.getElementById("nota").innerHTML=getItensToString();
        }

        function getItensToString()
        {
            var result  = '';

            for (i = 0; i < itens.length; i++){
                result = result + itens[i]['nomeItem'] + ' ' + itens[i]['quantidade'] + '\r\n';
            }
            return result;
        }

        adicionaItem('cafe', 10);
        adicionaItem('arroz', 20);
    </script>
</head>
<body>
    <textarea id="nota" name="nota"></textarea>
    <button type=button" onclick="concluirCompra();">Carregar</button>
</body>
</html>

Explanation: Since the variable items is an array of positions, you only have to scan each position to display the data in textarea, with a% wrap of%.

Reference

07.04.2014 / 01:16