How to insert an array within a text area?

Asked

Viewed 980 times

1

How to insert an array within 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
}
  • 2

    Insert how? You can edit the question with an example of the content you want to see in the textarea?

  • I don’t understand what you really want. You want to add the names or id’s of the items in a text?

  • A textarea is a text box, if you want to insert a vector into a text box it doesn’t make much sense without saying how your vector should appear in text form. It would be the same as asking to make the drawing of love on paper.

  • It would be as if it were a purchase note, with product name and quantity, would be a matrix in the case

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: As the variable items is an array of positions just scan each position to show the data in the textarea, with a line break \r\n.

Reference

Browser other questions tagged

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