0

Hello I want to find and replace using jquery, my code is like this:

<input id="find" style="width:100%; height:100%;" value="apple!" />

<input id="replace" style="width:100%; height:100%;" value="apples!" />

<textarea id="input" rows="4" style="width:150px; height:100px; resize:none;" wrap="off">Give me some apple</textarea>

Find: apple!

Replace with: apples!

Input Text: Give me some apple!

Result: Give me some apples!

2
  • 2
    have you tried anything from your side? On which event you want to replace text? Commented Mar 20, 2015 at 6:09
  • In which Event you want to do? Commented Mar 20, 2015 at 6:12

4 Answers 4

1

You want to replace apple! with apples! but in your text area you have only apple and not apple! (with exclamation mark). So please add ! to your text area or remove from input fields and try below code

$(function(){
    $('#replaceBtn').click(function(){
         var findText = $('#find').val();
         var replaceText = $('#replace').val();
         var inputText = $('#input').val();
         inputText = inputText.replace(findText, replaceText);
         $('#input').val(inputText);
    });
});

JSFiddle Demo

NOTE - Assuming that you are doing this replace on some button click. I have used Replace button in my demo. Also note that this replace is case sensitive.

EDIT - Above solution will replace only first occurance of word, to replace all occurrences you need use RegEx hence replace below line

inputText = inputText.replace(findText, replaceText);

with

inputText = inputText.replace(new RegExp(findText, 'g'), replaceText);

JSFiddle Demo with RegEx

Sign up to request clarification or add additional context in comments.

3 Comments

this will replace only the first apple! word. but if it has more than one apple! words this is not going to work.
@Iceburg, I agree with you. Please check my edit in my answer posted. Thanks for pointing out the issue :)
you are welcome.. and below i have posted the edited version of yours. :)
0
 $("body").find("#input").replaceWith("");

I don't known what exactly u want to replace if u want to replace the "Give me some apple" then u can go with this code or if u want to some thing insted of "give me some apple" then u can paas the text inside the replaceWith();

Comments

0

Try this code

var find =$("#find").val();
var replace = $("#replace").val();
if($("#input").val().contains(find))
{
    $("#input").val($("#input").val().replace(find,replace));
}

Demo

Comments

0

small modification to Bhushan Kawadkar's code block. and this will replace all the apple! words in single click.

$(function(){
 $('#replaceBtn').click(function(){
     var findText = $('#find').val();
     var replaceText = $('#replace').val();
     var textarea = $('#input');
   textarea.val(textarea.val().replace(new RegExp(findText,"g"),replaceText)); 
  });
 });

the "g" modifier will performs a global search.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.