user12937576
user12937576

Reputation:

getting a value of HTML

Im learning JavaScript and Im having problems trying to get the value of HTML of textareaElement. Theres lots online and because of all the information available its making it more confusing. I understand the idea behind the DOM, but not sure how to do the code. I am also trying to use add an event listener to store data in local storage, but without any luck.

// 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);

}   

HTML is:

<section id="text" class="button">
    <button type="button">Add entry</button>
</section>
<section id="image" class="button">
    <button type="button">Add photo</button>
    <input type="file" accept="image/*" />
</section>

[HTML][1]

Upvotes: 1

Views: 137

Answers (1)

Sudhanshu Garg
Sudhanshu Garg

Reputation: 159

'textareaElements' is not plural as you have it here:

var data = textareaElements.value;

This is the correct form:

var data = textareaElement.value;

Upvotes: 2

Related Questions