SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need help with a little scritp (find and replace text in textarea)

    what im trying to do is create a small script where the user can find and replace text that ive put into a text area. what i mean is a user types something into one textare1, it is higlighted in textarea2 which has text that i have written, and then he types something else in textarea3 which replaces whatever is in textarea2 that was highlighted..... i think this images explains it


  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    If you have to hit the submit button to make the changes - this can be done in PHP, if its on the fly - it needs to be something like javascript.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how could this be done in php. and would the page have to reload?

    would javascript be easier

    i would need whatever is changed in the bottom textarea to be submitable

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That should be handled by js obviously, sending data back and forth to the server just to do some text processing doesn't make much sense.

    Here's some basic code to highlight and replace. Highlight doesn't work on IE though.
    HTML Code:
    <textarea id="t1"></textarea>
    <textarea id="t2"></textarea>
    <input type="button" value="Find" onclick="find('t1','t3')" />
    <input type="button" value="Replace" onclick="replace('t1','t2','t3')" />
    <br />
    <textarea id="t3"></textarea>
    Code javascript:
    function find(find,source){
      var t = document.getElementById(source);
      var str = document.getElementById(find).value;
      var i = t.value.search(str);
      if(t.setSelectionRange && i>-1){
        t.setSelectionRange(i,i+str.length);
        t.focus();
      }
    }
    function replace(find,replace,source){
      var t = document.getElementById(source);
      var str = document.getElementById(find).value;
      var r = document.getElementById(replace).value;
      t.value=t.value.replace(str,r);
    }
    Saul

  5. #5
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    398
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that perfect man thanks


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
  •