• Carnegie Mellon University
  • 5000 Forbes Avenue, Pittsburgh, PA 15213...">

    Entering selected text with tags

    <ul class="contact"> <li class="first">Carnegie Mellon University</li> <li>5000 Forbes Avenue, Pittsburgh, PA 15213</li> </ul> 

    =>

     <ul class="contact"> <li class="first">Carnegie Mellon University</li> <li>[address]5000 Forbes Avenue, Pittsburgh, PA 15213[/address]</li> </ul> 

    Imagine that I open a web page in a browser and find useful information on the page. Therefore, I would like to select useful information (for example, Carnegie Mellon University address) from the page and click the "Insert Tag" button added to TO-BE-IMPLEMENTED. As a result, the page source code will be provided with a pair of tags (for example, [address] [/ address]) surrounding the text selected by the user. Then I will save the entered source code for further processing.

    Can someone help suggest a way to implement such a function? I am really new to Javascript, so please be a little detailed with your suggestion.

    0
    source share
    1 answer

    Searching on google, I find an example where you can get the highlighted text: http://mark.koli.ch/2009/09/use-javascript-and-jquery-to-get-user-selected-text.html .

    In this case, you only need to change the warning action to insert the tags you want. To do this using jquery, use the following code:

     $(selected).text("[tag]" + $(selected).text() + "[/tag]"); 

    where selected should be a variable representing the text you have selected.

    0
    source

    All Articles