Reputation: 1958
I got a variable using document.getSelection()
This variable is well displayed if I use alert()
but not if I use html()
.
How can I make it visible with html()
?
$(document).ready(function(){
$(".sentence").dblclick(function(){
var selected_word = document.getSelection();
$("#word_to_be_showned_in").html(selected_word);
alert(selected_word);
});
});
<p class="sentence">have a try</p>
<p>Selected word should appear here: <span id="word_to_be_showned_in">XXX</span></p>
Example (compatible with chrome): http://js.do/code/38012
Upvotes: 2
Views: 88
Reputation: 478
Add to the var a empty string and it will treat as string.
Example:
var+""
Live Demo of your code at http://jsfiddle.net/U5nWV/
Upvotes: 1
Reputation: 36438
getSelection()
returns an object, not a string. Add .toString()
to get its text:
var selected_word = document.getSelection().toString();
$("#word_to_be_showned_in").html(selected_word);
alert(selected_word);
Fixed example: http://js.do/code/38017
Upvotes: 4