Click here to Skip to main content
15,947,411 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi. I am very new to JavaScript and I am trying to make an event listener to save text when it changes. I am trying to get the value of the entries, make a text item from the values and store in local storage. then connect onblur.

Any help with trying to figure it out would be great. Especially trying to get the value of HTML document.


HTML is:
```
HTML
<section id="text" class="button">
    Add entry
</section>
<section id="image" class="button">
    Add photo
    
</section>
``


What I have tried:

JavaScript
// Add a text entry to the page
      function addTextEntry(key, text, isNewEntry) {
      // Create a textarea element to edit the entry
      var textareaElement = document.createElement("TEXTAREA");
      textareaElement.rows = 5;
      textareaElement.placeholder = "(new entry)";
      
    
      // Set the textarea's value to the given text (if any)
      textareaElement.value = text;
    
      // Add a section to the page containing the textarea
      addSection(key, textareaElement);
    
      // If this is a new entry (added by the user clicking a button)
      // move the focus to the textarea to encourage typing
      if (isNewEntry) {
        textareaElement.focus();
    
    // Get HTML input values
    var data = textareaElement.value;

}
  
  

    // ...get the textarea element's current value
      var data = textareaElement.value;
     
      // ...make a text item using the value
      var item = makeItem("text", data);
      // ...store the item in local storage using key
      localStorage.setItem(key, item);
      // Connect the event listener to the textarea element:
      textareaElement.addEventListener('onblur', addTextEntry);
    
    }
Posted
Updated 24-Feb-20 1:33am
v4
Comments
ZurdoDev 24-Feb-20 7:25am    
Where are you stuck specifically?

1 solution

im trying to figure out how to connect the event listener to the textarea element. Theres already a function initialize () at the end of the file to start up when the page loads, but when i add the code below i cant get it to work.

textareaElement.addEventListener('onblur', addTextEntry);
 
Share this answer
 
Comments
Richard MacCutchan 24-Feb-20 8:02am    
This is not a Solution. If you want to reply to a comment then please use the Reply button on the comment. If you want to add more detail to your post then use the Improve question link. But either way, saying, "i cant get it to work." does not provide us with any useful information. You need to explain what that means and where the failure occurs.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900