Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6696735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:22:35+00:00 2026-05-26T06:22:35+00:00

Would anyone have any idea how to make a Find and Replace thing where

  • 0

Would anyone have any idea how to make a Find and Replace thing where when you can just click next it will bring you to the next found item?

Edit:

For an Textarea. I want a Javascript code that can add a Find and Replace to Textarea. I don’t wanna just use.

search()

or Replace().

At the Moment im trying this:

function allIndexes() {
var indices = new Array();
var index = 0;
var i = 0;
while(index = $('#text').val().indexOf($('#search').val(), index) > 0) {
indices[i] = index;
i++;
}
return indices;
}

But that doesn’t work at all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:22:36+00:00Added an answer on May 26, 2026 at 6:22 am

    I updated my earlier answer and finished the search and replace functionality based on the direction my earlier post outlined. I tested this in Chrome 14, IE8 and Firefox 3.6.

    Find: will select the next instance of the searchterm.

    Find/Replace: will replace the next occurrence of the search string and select the replacement

    Replace All: will replace all occurences and select the piece of text that has been replaced last

    Hope this is now, what you were looking for. You should definitely be able to go from here and style this or make a proper class out of this…

    <script src="jquery.js" type="text/javascript"></script>    
    
    <textarea id="txtArea" style="width: 300px; height:100px">
        This is a test. A test, i say. The word TEST is mentioned three times.
    </textarea>
    
    <p>
        <label for="termSearch">Search</label>
        <input type="text" id="termSearch" name="termSearch" value="test" /><br/>
        <label for="termReplace">Replace</label>
        <input type="text" id="termReplace" name="termReplace" value="solution" /><br/>
        <label for="caseSensitive">Case Sensitive</label>
        <input type="checkbox" name="caseSensitive" id="caseSensitive" /><br/>
        <a href="#" onclick="SAR.find(); return false;" id="find">FIND</a><br/>
        <a href="#" onclick="SAR.findAndReplace(); return false;" id="findAndReplace">FIND/REPLACE</a><br/>
        <a href="#" onclick="SAR.replaceAll(); return false;" id="replaceAll">REPLACE ALL</a><br/>
    </p>
    
    <script type="text/javascript">
        var SAR = {};
    
        SAR.find = function(){
            // collect variables
            var txt = $("#txtArea").val();
            var strSearchTerm = $("#termSearch").val();
            var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
    
            // make text lowercase if search is supposed to be case insensitive
            if(isCaseSensitive == false){
                txt = txt.toLowerCase();
                strSearchTerm = strSearchTerm.toLowerCase();
            }
    
            // find next index of searchterm, starting from current cursor position
            var cursorPos = ($("#txtArea").getCursorPosEnd());
            var termPos = txt.indexOf(strSearchTerm, cursorPos);
    
            // if found, select it
            if(termPos != -1){
                $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length);
            }else{
                // not found from cursor pos, so start from beginning
                termPos = txt.indexOf(strSearchTerm);
                if(termPos != -1){
                    $("#txtArea").selectRange(termPos, termPos+strSearchTerm.length);
                }else{
                    alert("not found");
                }
            }
        };
    
        SAR.findAndReplace = function(){
            // collect variables
            var origTxt = $("#txtArea").val(); // needed for text replacement
            var txt = $("#txtArea").val(); // duplicate needed for case insensitive search
            var strSearchTerm = $("#termSearch").val();
            var strReplaceWith = $("#termReplace").val();
            var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
            var termPos;
    
            // make text lowercase if search is supposed to be case insensitive
            if(isCaseSensitive == false){
                txt = txt.toLowerCase();
                strSearchTerm = strSearchTerm.toLowerCase();
            }
    
            // find next index of searchterm, starting from current cursor position
            var cursorPos = ($("#txtArea").getCursorPosEnd());
            var termPos = txt.indexOf(strSearchTerm, cursorPos);
            var newText = '';
    
            // if found, replace it, then select it
            if(termPos != -1){
                newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length)
                $("#txtArea").val(newText);
                $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length);
            }else{
                // not found from cursor pos, so start from beginning
                termPos = txt.indexOf(strSearchTerm);
                if(termPos != -1){
                    newText = origTxt.substring(0, termPos) + strReplaceWith + origTxt.substring(termPos+strSearchTerm.length, origTxt.length)
                    $("#txtArea").val(newText);
                    $("#txtArea").selectRange(termPos, termPos+strReplaceWith.length);
                }else{
                    alert("not found");
                }
            }
        };
    
        SAR.replaceAll = function(){
            // collect variables
            var origTxt = $("#txtArea").val(); // needed for text replacement
            var txt = $("#txtArea").val(); // duplicate needed for case insensitive search
            var strSearchTerm = $("#termSearch").val();
            var strReplaceWith = $("#termReplace").val();
            var isCaseSensitive = ($("#caseSensitive").attr('checked') == 'checked') ? true : false;
    
            // make text lowercase if search is supposed to be case insensitive
            if(isCaseSensitive == false){
                txt = txt.toLowerCase();
                strSearchTerm = strSearchTerm.toLowerCase();
            }
    
            // find all occurances of search string
            var matches = [];
            var pos = txt.indexOf(strSearchTerm);
            while(pos > -1) {
                matches.push(pos);
                pos = txt.indexOf(strSearchTerm, pos+1);
            }
    
            for (var match in matches){
                SAR.findAndReplace();
            }
        };
    
    
        /* TWO UTILITY FUNCTIONS YOU WILL NEED */
        $.fn.selectRange = function(start, end) {
            return this.each(function() {
                if(this.setSelectionRange) {
                    this.focus();
                    this.setSelectionRange(start, end);
                } else if(this.createTextRange) {
                    var range = this.createTextRange();
                    range.collapse(true);
                    range.moveEnd('character', end);
                    range.moveStart('character', start);
                    range.select();
                }
            });
        };
    
        $.fn.getCursorPosEnd = function() {
            var pos = 0;
            var input = this.get(0);
            // IE Support
            if (document.selection) {
                input.focus();
                var sel = document.selection.createRange();
                pos = sel.text.length;
            }
            // Firefox support
            else if (input.selectionStart || input.selectionStart == '0')
                pos = input.selectionEnd;
            return pos;
        };  
    </script>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone have any idea how I would go about converting a timestamp in milliseconds
Anyone have any idea why shuffle() would only return 1 item? when using: $array2
Does any one have any idea why this would be happening? This is some
Does anyone have any idea if there is anything planned in the html5 or
Does any one know how I would have to change the following to work
Does anyone have experience with algorithms for evaluating hypergeometric functions? I would be interested
curious if anyone might have some insight in how I would do the following
I was wondering if anyone would help with the following: I have a date
This is going to seem extremely trivial, but it just doesn't make any sense
Does anyone have a hello world sample or tutorial for creating an Eclipse plugin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.