qadenza
qadenza

Reputation: 9293

cannot get the content of active div

<div class="wrap3">
lorem lorem lorem
<div class="inside3 ed" contentEditable>ipsum ipsum ipsum</div>
</div>

<div id="btnsave">Save</div>

js

$("#btnsave").click(function(){
    var a = document.activeElement.innerText;
    console.log(a);
});

Click inside ed it becomes active - (blue border arround)

Click on btnsave does not give the content of ed but the content of entire document.

Upvotes: 0

Views: 98

Answers (2)

Brian Mock
Brian Mock

Reputation: 41

I was able to get it to work correctly with a change of the save element to a button: https://jsfiddle.net/eac5L0aa/1/

<div class="wrap3">
lorem lorem lorem
<div class="inside3 ed" contentEditable>ipsum ipsum ipsum</div>

</div>

<button id="btnsave">Save</button>

$("#btnsave").click(function(){
    var a = getSelectionStart();
    console.log(a);
});
function getSelectionStart() {
   var node = document.getSelection().anchorNode;
   console.log(node);
   return (node.nodeType == 3 ? node.parentNode : node);
}

Credit to this answer

Upvotes: 1

Rino Raj
Rino Raj

Reputation: 6264

$("#btnsave").click(function(){
    var a = $('.ed:focus').text();
});

I think this will do.

Another solution:

$("#btnsave").click(function(){
     $('.lastEdited').text();
     alert($('.lastEdited').text())
});
$("div[contentEditable]").click(function(){
    $("div[contentEditable]").removeClass('lastEdited');
    $(this).addClass('lastEdited');
});

DEMO

Upvotes: 2

Related Questions