Javascript Form How to - Assign table content to textarea








Question

We would like to know how to assign table content to textarea.

Answer


<!DOCTYPE html>
<html>
<body>
  <table>
    <tr>
      <form name="foo" action="" method="get">
        <td id="td">text: <input name="k" type="text" />
        </td>
      </form>
    </tr><!--from  w  w  w .  java2 s.  c  o  m-->
  </table>
  <textarea rows="20" cols="50"></textarea>
  <script type='text/javascript'>
    var table = document.querySelector('table');
    document.querySelector('textarea').value = table.innerHTML;
    
    </script>
</body>
</html>

The code above is rendered as follows: