javascript - window.getselection() not working in FF and chrome -


i have texbox in page , trying length textbox. know how length in ie, following code not working in ff , chrome.

<!doctype html> <html> <head> <script> function myfunction(obj) { alert("mouse up"); var r=window.getselection().createrange(); alert(r.text.length);  } </script> </head> <body>  <textarea id="myarea" cols="30" spellcheck="false" onmouseup=myfunction(this)>select text within field.</textarea>   </body> </html> 

textareas , text inputs have different selection api main document selection. use selectionstart , selectionend properties of textarea/input.

function myfunction(obj) {     var selectedtext = obj.value.slice(obj.selectionstart, obj.selectionend);     alert(selectedtext); } 

if need support ie <= 8, there different api again. see caret position in textarea, in characters start


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -