Advanced Search

Results 1 to 3 of 3

Thread: Select text without current value

  1. #1
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Exclamation Select text without current value

    Hello,

    I was wondering if you can do a script where when you type in a textarea and click on a button it has the value plus something else and minus the current value.

    For example I'm typing text and then i want a <b> code around it replacing the original.

    I want this:

    <b>text text text</b>

    Not this:

    text text text<b>text text text</b>


    Here's what I'm doing right now

    HTML Code:
    <!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">
    <head>
    <title>Untitled Document</title>
    <script>
    function insert(tag,endtag)	{
    
    var text = ""
    var currentValue = document.form1.html.value
    text = (document.all) ? document.selection.createRange().text : document.getSelection();
    document.form1.html.value += tag + text + endtag
    return true;
    }
    </script>
    </head>
    
    <body>
    <div class="mainbody">
      <form id="form1" name="form1" method="post" action="">
        <textarea name="html" cols="100" rows="15">Type here...</textarea>
                <input name="bold" type="button" id="bold" value="Bold" onclick="insert('<b>','</b>')"/>
            </form>
    
    </div>
    </body>
    
    </html>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  2. #2
    Join Date
    Jun 2006
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Hi,

    You could use the following code as below , to get the bold tag been placed inbetween the text of the TextArea.

    <!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">
    <head>
    <title>Untitled Document</title>
    <script>
    function Bold()
    {
    var formData = document.selection.createRange().text;
    if (formData != '')
    {
    document.selection.createRange().text = '<b>'+formData+'</b>';
    }
    else
    {
    alert("Select the data to be get bold");
    }
    }
    </script>
    </head> <body> <div class="mainbody">
    <form id="form1" name="form1" method="post" action="">
    <textarea name="html" cols="100" rows="15">Type here...</textarea>
    <input name="bold" type="button" id="bold" value="Bold" onclick="Bold()" />
    </form>
    </div>
    </body>
    </html>


    Let me know if u have any doubts.

    Regards,
    Valli

  3. #3
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Thanks! Now i'll just edit that....
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •