-3
<!doctype html>
<html>
    <head>
        <title>My test</title>
        <link rel="stylesheet"  type="text/css" href="editor/style.css">           
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>           
    </head>
   <body>
        <div contenteditable="true" class="doc">
            <p contenteditable="true" class="letters" >My editor</p>
        </div>
        <button id="but">Done Editing</button> 

        <script>
            $(document).ready(function() 
            {
                $('#but').on('click',function(){
                    //want to obtain all the text within the contenteditable div on clicking the button
                });
            });
        </script>
    </body>
</html>

On clicking the "Done Editing" button I want to get all the text which is entered in my contenteditable div. Apparently,commands such as

var content = $('.doc').text();
Kaustubh
  • 117
  • 2
  • 14
  • Possible duplicate of [Get html content of a selector ( .html() ) with jquery from a javascript variable that contains HTML](http://stackoverflow.com/questions/21622412/get-html-content-of-a-selector-html-with-jquery-from-a-javascript-variabl) – Sagar V Mar 27 '17 at 14:30

3 Answers3

1

Your div does not have id="my-contenteditable-div", that is why it does not work.

You can access it this way using jQuery:


$(document).ready(function() {
  $('#but').on('click', function() {
    console.log($('#myDiv').html());
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="myDiv" contenteditable="true">DIV CONTENT</div>
<button id="but">Done Editing</button>

Other alternatives here

Community
  • 1
  • 1
Mistalis
  • 17,793
  • 13
  • 73
  • 97
1

You should use a valid selector. As the div has a class, use the class selector $('.doc')

To know more about selectors, refer to the api docs

<!doctype html>
<html>

<head>
  <title>My test</title>
  <link rel="stylesheet" type="text/css" href="editor/style.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

</head>

<body style="overflow: hidden; margin-top: 50px">
  <div contenteditable="true" class="doc">
    <p contenteditable="true" class="letters">My editor</p>
  </div>
  <button id="but">Done Editing</button>

  <script>
    $(document).ready(function() {
      $('#but').on('click', function() {
        console.log($('.doc').text());
      });
    });
  </script>
</body>

</html>
Graham
  • 7,431
  • 18
  • 59
  • 84
Pugazh
  • 9,453
  • 5
  • 33
  • 54
1

Use the appropriate selector .letters

$(document).ready(function() {
      $('#but').on('click', function() {
          var content = $('.letters').html();
            console.log(content);
            // do the process here
          });
      });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!doctype html>
<html>

<head>
  <title>My test</title>
  <link rel="stylesheet" type="text/css" href="editor/style.css">

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

</head>

<body style="overflow: hidden; margin-top: 50px">
  <div contenteditable="true" class="doc">
    <p contenteditable="true" class="letters">My editor</p>
  </div>
  <button id="but">Done Editing</button>
</body>

</html>
Sagar V
  • 12,158
  • 7
  • 41
  • 68
  • `$('p.letters').html()` or `$('.letters').html()` or both are fine to use? I think that `$('p.letters').html()` is more specific. – NightKn8 Mar 27 '17 at 14:16
  • BY using this text if I want to extract the text between anchor tags,anything between these? – Kaustubh Mar 27 '17 at 14:20
  • yes @Kaustubh you can the `text` will return the `textContent` whereas the `html` will return the `innerHTML`. FYI, `text()` on an HTML node will give many unwanted space, which you want to trim then. – Sagar V Mar 27 '17 at 14:22