-1

I have this function:

  function AddRowToForm(row){
  $("#orderedProductsTblBody tr").each(function(){
  // find the first td in the row
  arr.push($(this).find("td:first").text());
  });
  for (i=0;i<arr.length;i++)
  // display the value in input field
document.getElementById("message").innerHTML = (arr[i] + "<br >");
 });
} 

The problem is that I have to retrieve the array in an input field into a form. The input field is the last one (id="message")

 fieldset class="pure-group">
  <label for="date">Data: </label>
  <input id="date" name="date" type="text"/>
</fieldset>

<fieldset class="pure-group">
  <label for="time">Ora: </label>
  <input id="time" name="time" />
</fieldset>

<fieldset class="pure-group">
  <label for="pax">Numero di Persone: </label>
  <input id="pax" name="pax" />
</fieldset>

<fieldset class="pure-group">
  <label for="message">Esperienze: </label>
  <input id="message" name="message" rows="5"></input>
</fieldset>

The problem is that the function doesn't work. I tried with document.write (instead of innerHTML and it works but it cancel everything. Any help? Thank you in advance!:

1 Answers1

0

SO you have a couple of issues. You set the text of an input with value. You are also not appending, you are just overriding the value. For a textarea, you need to use \n for new lines.

$("#orderedProductsTblBody tr").each(function(){
   arr.push($(this).find("td:first").text());
});
for (var i=0;i<arr.length;i++)
   // display the value in input field
   document.getElementById("message").value += arr[i] + "\n");
});

Now how would I do it? Just use join.

document.getElementById("message").value = arr.join("\n");
epascarello
  • 204,599
  • 20
  • 195
  • 236