SitePoint Sponsor

User Tag List

Results 1 to 1 of 1
  1. #1
    SitePoint Zealot DavidR's Avatar
    Join Date
    Feb 2002
    London, England.
    0 Post(s)
    0 Thread(s)

    Question How to Search and replace in a <textarea>?


    Does anyone know if a code exists to do the following... or can help me out ?

    I'd like to have a <textarea> with text in it that can be personalised similar to search and replace in notepad. But there is no need for a search, just replace. So Value X can be replace by whatever is inputted.

    I have mocked up a demo to explain what I mean... obviously this doesn't actually work

    So, somehow (I'm clearly no Javascript genius) X will be repalced with David in the textarea when the Replace button is hit.

    Someone helped me out a while ago with the code below, which adds text to a textarea when a checkbox is click, can this be of any use or adapted? Maybe not, just wondering!

    Code JavaScript:
    [color=#000080]<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">[/color]
            [color=#000080]<meta http-equiv="content-type" content=[color=#0000FF]"text/html;charset=iso-8859-1"[/color]>[/color]
            [color=#000080]<title>[/color]Untitled Page[color=#000080]</title>[/color]
            [color=#800000]<script type=[color=#0000FF]"text/javascript"[/color]>[/color][i][color=#000080]<!--
    function addRemoveText(checked) {
        textarea = document.getElementById("myTextarea");
        phrase = " The checkbox is checked.";
        if (checked && !textarea.value.match(phrase)) {
            textarea.value += phrase;
        } else {
            textarea.value = textarea.value.replace(phrase,"");
            [color=#FF8000]<textarea id=[color=#0000FF]"myTextarea"[/color] name=[color=#0000FF]"myTextarea"[/color] rows=[color=#0000FF]"4"[/color] cols=[color=#0000FF]"40"[/color]>[/color][color=#FF8000]</textarea>[/color][color=#000080]<br/>[/color]
            [color=#FF8000]<input type=[color=#0000FF]"checkbox"[/color] name=[color=#0000FF]"checkboxName"[/color] value=[color=#0000FF]"checkboxValue"[/color] onclick=[color=#0000FF]"addRemoveText(this.checked);"[/color]>[/color] Add Text

    Thanks for any help!

    Edit: If someone can make me something like this I'd be willing to pay, am I allowed to say that or do I have to post it in the selling/buying area? Ah well, I said it!
    Last edited by DavidR; Sep 6, 2007 at 14:09.


Posting Permissions

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