I've got some code that reads highlighted text. But I'm having trouble assigning a variable to the function in JavaScript. It does not call the function properly as expected.
<html>
<head>
<script type="text/javascript">
function getSelectionText()
{
var text = "";
if (window.getSelection)
{
text = window.getSelection().toString();
}
return text;
}
var txt = getSelectionText(); //<-----This is not working???
</script>
</head>
<body>
<p id="p1">Select some of this text then press the button<br /></p>
<button onclick= document.write(txt) >GetText</button>
</body>
</html>
If I use the function in the write params it works.
<button onclick= document.write(getSelectionText()) >GetText</button>
Why doesn't the function get called properly if I assign a variable to it?
-ScottA
txtbefore the page loads.