Web Analytics Made Easy -
StatCounter JS replace function - CodingForum


No announcement yet.

JS replace function

  • Filter
  • Time
  • Show
Clear All
new posts

  • JS replace function

    Hi all,
    I am trying to implement a function whereby the user selects some text and on click of a button a set of span tags are wrapped around that selected text. If the text contains within it another set of tags(for example a href) it will surround the text with span tags aswell.

    <p> I went to the market yesterday to buy some <a href="url">fruit and vegetables</a> and then walked home instead catching the car </p> <p> The next day I did the same thing </p>

    So if the user selects from "I went to the" to "The next day" it would look something like this:

    <p><span color = 'blue'> I went to the market yesterday to buy some </span> <a href="url"><span color = 'blue'>fruit and vegetables</span></a><span color = 'blue'> and then walked home instead catching the car<span> </p> <p><span color = 'blue'> The next day </span color=blue> I did the same thing </p>

    Any idea how I can do this?


    Kind regards


  • #2
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <title>document.selection 的 createRange</title>

    <div>please select me<div>
    <div><input type="button" value="makeBold" onclick="javascript:Bold();" /></div>
    <script type="text/javascript" language="javascript">
    function Bold()
    var r = document.selection.createRange();
    //use this object to get select area;