1
  1. I'm able to pass the displayMessage variable, but the page reloads. I've been looking at ajax tutorials, but I'm not sure how to pass displayMessage without the page reloading.

  2. I want to store more than one message on the phone. So if I send "help me stackoverflow," then "please," they both get stored. (right now "please" would replace "help me...").

    // this is my form
    <form role="form">
    <div class="form-group">
    <textarea class="form-control" id="message" rows="3"></textarea>
    </div>
    <button type="submit" id="submit" class="btn btn-default">Submit</button>
    </form>
    
    
    // this is where message displays
    <span class="displayMessage"></span>
    
    
    // this is my js
    $(document).ready(function () {     
      $('#submit').click(function () {
          var displayMessage = $('#message').val();
          localStorage.displayMessage = displayMessage;
      });
    
    });
    
      $(document).ready(function () {
      $('.displayMessage').html(localStorage.displayMessage);
    })
    

What my screen looks like.

DontVoteMeDown
  • 21,122
  • 10
  • 69
  • 105
evan
  • 954
  • 3
  • 18
  • 37

1 Answers1

2
  1. The page reload is triggered by the default button click behaviour. You need to disable it by calling preventDefault on the event object:

    $('#submit').click(function (e) {
        e.preventDefault();
        // your code
    });
    
  2. As DontVoteMeDown already said, you can store an object or array as JSON in the local storage. So you could do something like this:

    $(document).ready(function () {     
        $('#submit').click(function (e) {
            e.preventDefault();
    
            var storedMessagesJSON = localStorage.displayMessages || '[]',
                storedMessages = JSON.parse(storedMessagesJSON),
                displayMessage = $('#message').val();
    
            storedMessages.push(displayMessage);
            localStorage.displayMessages = JSON.stringify(storedMessages);
        });
    
        var storedMessagesJSON = localStorage.displayMessages || '[]',
            storedMessages = JSON.parse(storedMessagesJSON);
        $('.displayMessage').html(storedMessages.join('<br>'));
    });
    

    I also made a JSFiddle.

Jan
  • 1,394
  • 10
  • 12
  • Nice! thank you. Now I have two problems which I've made this fiddle for: http://jsfiddle.net/WqDJv/. The
    tags push down the iphone, and the messages are all in the same css bubble, instead of creating a new one each time. And... the clear button doesn't work.
    – evan Dec 18 '13 at 20:20
  • The iPhone image is pushed down because it is below the messages. You can make the messages `position: absolute`, which will prevent them to incluence other content. I don't know why the click event of the clear button is not correctly fired. Nevertheless you should also use jQuery to bind the click event to the button. I updated [your fiddle](http://jsfiddle.net/WqDJv/1/). – Jan Dec 18 '13 at 22:29