How to get selected text on click of an element in javascript -
i have div [contenteditable=true]
attribute. when select text , click button, want selected text. did following:
$(".btn").click(function (){ alert(window.getselection()); });
the above code not alert anything. if change .btn
element img
element, alert outputs selected text. can selected text on click of element
thid html code:
<li class="btn">selection </li> <div contenteditable="true">some text here</div>
when select required text , click button, ramon & adeneo explains in comment, focus lost. button command executed @ mouseup
at point selection cleared. far understand, reason why code works image because unlike button image command executed @ mousedown
.
my suggestion try .on('mousedown', ...)
command instead of simple click command.
Comments
Post a Comment