Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to wrap html tag for jquery mouseup selection wrods?
    primarykey
    data
    text
    <p>I have reference the answer here <a href="https://stackoverflow.com/questions/7380190/select-whole-word-with-getselection">Select whole word with getSelection</a>. I would like to make a <code>mouseup</code> selection <code>completed words</code> from <code>body</code>. than wrap <code>&lt;p&gt;</code> tag for the <code>selection words</code>. Here is the code. So how to? Thanks.</p> <pre><code>&lt;script src="jquery.js"&gt;&lt;/script&gt; &lt;script&gt; (function($) { function getSelected() { if(window.getSelection) { return window.getSelection(); } else if(document.getSelection) { return document.getSelection(); } else { var selection = document.selection &amp;&amp; document.selection.createRange(); if(selection.text) { return selection.text; } return false; } return false; } function expand(range) { if (range.collapsed) { return; } while (range.toString()[0].match(/\w/)) { range.setStart(range.startContainer, range.startOffset - 1); } while (range.toString()[range.toString().length - 1].match(/\w/)) { range.setEnd(range.endContainer, range.endOffset + 1); } } $(document).ready(function() { $('body').mouseup(function() { var selectionRange = getSelected().getRangeAt(0); var start = selectionRange.startOffset; expand(selectionRange); var selection = selectionRange.toString(); if(selection &amp;&amp; (selection = new String(selection).replace(/^\s+|\s+$/g,''))) { //how to wrap &lt;p&gt; tag for the selection words? } }); }); })(jQuery); &lt;/script&gt; &lt;style&gt; p {color:blue; } &lt;/style&gt; &lt;body&gt; Facebook needs to scrape your page to know how to display it around the site. Facebook scrapes your page every 24 hours to ensure the properties are up to date. The page is also scraped when an admin for the Open Graph page clicks the Like button and when the URL is entered into the Facebook URL Linter. Facebook observes cache headers on your URLs - it will look at "Expires" and "Cache-Control" in order of preference. However, even if you specify a longer time, Facebook will scrape your page every 24 hours. The user agent of the scraper is: "facebookexternalhit/1.1 (+http://www.facebook.com/externalhit_uatext.php)" &lt;/body&gt; </code></pre> <p>UPDATE:</p> <pre><code>var new_html = $('body').html().split(selection, 1)[0] + '&lt;p&gt;' + selection + '&lt;/p&gt;' + $('body').html().split(selection, 2)[1]; $('body').html(new_html); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload