0

I'm trying to set a textarea value from localStorage in a handlebars template but have had no luck. The code works fine as soon as I take it out fo the template tags, but within it does nothing. From what I've read setting on document ready should work, but for me it's not.

I'm trying to store the comment whilst the user goes away and logs in or registers.

Here's the code I have:

$(document).ready(function () {
  if (localStorage['comment_text']) {
    var user_comment_text = localStorage['comment_text'];
    $('textarea#comment_text').val(user_comment_text);
  }
});

Am I doing something stupid here?

Edit

When logged to console it is getting the value from localStorage. Just doesn't set it in the textarea. I'm guessing it's to do with the 'textarea#comment_text' not being available when the javascript is being executed. I thought calling on document ready would solve this.

jholloman
  • 1,959
  • 14
  • 16
Bunkered
  • 309
  • 1
  • 3
  • 12
  • 1
    Don't you need to use localStorage.getItem(key) to retrieve values from localStorage? – TommyBs Jan 29 '13 at 15:51
  • 1
    I think you can get it either way. Tried both but to no avail. – Bunkered Jan 29 '13 at 15:55
  • 1
    On a side note, don't do this: $('textarea#comment_text') do this $('#comment_text') By prefixing with textarea you make the selector very inefficient as it searches the dom for every textarea element and then searches that list for the specified id. Id's are unique so just use that. – jholloman Jan 29 '13 at 16:03
  • Thanks, I din't have it in initially. Just trying everything and anything:) – Bunkered Jan 29 '13 at 16:07
  • It isn't clear from your question, but it sounds like you have a template containing a ` – Quentin Jan 29 '13 at 16:14
  • @Bunkered cool, not seen that anywhere so useful to know – TommyBs Jan 29 '13 at 17:56
  • @Quentin thanks but I'm not trying to execute script within innerHTML. I'm trying access a selector which is injected into the page via script (the handlebars template). It's definitely not a duplicate of that question. – Bunkered Jan 30 '13 at 09:52
  • @Bunkered — I'm still not clear on what you are actually trying to do. I recommend putting a reduced test case on jsbin or jsfiddle. – Quentin Jan 30 '13 at 09:55
  • @Quentin I created a jsfiddle and it works how I would expect [jsFiddle](http://jsfiddle.net/bunkered/XSUu3/). I've played around with the code a little more and got it to work by moving the code below the handlebars.compile, it was in a different file. I'll continue to try and find out why it didn't work, but for now I'm happy that it's working. – Bunkered Jan 30 '13 at 10:26

1 Answers1

0

Your edit is a correct assumption. There is nothing wrong with the provided code. Try this:

$(document).ready(function () {
  if (localStorage['comment_text']) {
    var comment_text = $('#comment_text');
    var user_comment_text = localStorage['comment_text'];
    debugger;
    comment_text.val(user_comment_text);
  }
});

Inspect comment_text and see what your selection is providing you with.

jholloman
  • 1,959
  • 14
  • 16
  • comment_text is returning e.fn.e.init[0], just looking into this now. Sorry, but I'm pretty new to javascript. – Bunkered Jan 29 '13 at 16:24