0

I have been testing the saving of cookies but cannot seem to crate any cookies, (I do not have a load button because i am using "Edit this cookie" to check for the cookies)

<html>
<body>
<table style="width:100%">
  <tr>
    <td>Texttest</td>
    <td>Numtest</td>
    <td>Dicetest</td>
  </tr>
  <tr>
    <td></td>
    <td><form> Numtest:
  <input type="number" name="numtest" min="0" max="100"> </form></td> 
    <td><button onclick="dice()">dicetest</button>
              <p><script>
function dice() {
var rolldice = Math.floor((Math.random() * 20) + 1);
document.getElementById("number").innerHTML = rolldice;
}
</script>
<p id="number"></p></td>
  </tr>
  <tr>
   <td><form method="post" action="">
<textarea name="textest" cols="35" rows="20" id="textest">
</textarea>
</form></td>
   <td><button onclick="save()">Savetest</button>
      <script>
          function save() {
              var TextT = document.getElementById("textest")
              
              document.cookie="saveinv="+TextT+"; expires=Thu, 18 Dec 3000 12:00:00 UTC";
              }
       </script> 
       </td>
      
   <td></td>
</table>
</body>
</html>
leppie
  • 115,091
  • 17
  • 196
  • 297
Milo
  • 1
  • 1
  • Are you running the code on webserver or your localhost? Cookies may not work correctly when opening HTML page and not running it on a webserver. – Igal S. Jun 18 '15 at 05:01
  • See also here: http://stackoverflow.com/questions/15914744/javascript-document-cookie-always-empty-string – Igal S. Jun 18 '15 at 05:01

1 Answers1

0

TextT is not a string it is a TextAreaElement. If you want TextT to contain the element's content, then access its value property:

var TextT = document.getElementById("textest").value;

But be careful! document.cookie="saveinv="+TextT+"...; could cause issues if the text area contains a semicolon.

Paul
  • 139,544
  • 27
  • 275
  • 264
  • Thank you for the help, but even with text in the text box I get the error `Uncaught TypeError: Cannot read property 'value' of null` – Milo Jun 18 '15 at 22:38