javascript - Cross-Browser way to select text -


I have a p element, how can I choose the text inside javascript Will be - browser?

  & lt; P & gt; Some text to choose from & lt; / P & gt;   

I want to select that text on click (like you use the mouse pointer and move the cursor) This can be a jQuery solution.

In the comment, I was able to write the jQuery plugin with the help of @ADNow, select the text $ .browser has been removed, so it's jQuery & gt; Works for 1.9.

  $ .fn.sel = function () {var node = this [0]; If (document.body.createTextRange) {var category = document.body.createTextRange (); Range.moveToElementText (node); Range.select (); } And if (window.getSelection) {var select = window.getSelection (); If (select.setBaseAndExtent) {selection.setBaseAndExtent (node, 0, node, 1); } And if (document.createRange) {var range = document.createRange (); Range.selectNodeContents (node); Selection.removeAllRanges (); Selection.addRange (range); }}};    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -