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

python - how we can use ajax() in views.py in django? -

matlab - Using loops to get multiple values into a cell -

python - Sequence Pattern recognition with Pybrain -