MPorto MPorto - 6 months ago 24
Javascript Question

Retrieving current text inside editable span tag

I am using Summernote (http://summernote.org/) to create an editor where users can edit a message they have created. Inside the text shown in the editor when the page loads, I have a few

<span>
tags that wrap some sections of the message, and each of these has a different id (for example,
<span id="something">
).

When the user presses the save button, I want to retrieve the text inside each
<span>
tag at that moment. I tried using something like
$("#something").text()
, but that only retrieves the text that was inside the spans when the page was loaded, not the current text. Any ideas how to fix this?

Answer

The summernote, create another span, if you use id you get the text of the element with display:'none', try to use class and get the last element

$(document).ready(function() {
  $('#summernote').summernote();
  console.log($('.test:last').text());
  $('.save').on('click', function() {
    console.log($('.test:last').text());
    return false;
  });
});

See the Plunker