0

i have $("#text") and i want to make it so that when you type the word 'replace me' into $("text"), it is automatically replaced with the phrase 'replaced successfully' and 'goodbye' with 'hello'

i'm assuming the right way to go about doing this would be to find 'replace' with regex and replace it and this is about as far as i've gotten

          $('#text').keydown(function(){
                var text = $('#text').text();
                var replaceCommand = ['replace me', 'goodbye'];
                var replaceOutput = ['replaced successfully', 'hello'];
                    for (var i=0; i < replaceCommand.length; i++){
                        text.replace(replaceCommand[i]/i, replaceOutput[i]);
                    }
            });
2
  • what is the problem, does it work or does replacement show up?? Commented Jul 24, 2012 at 10:15
  • it doesn't work, nothing is successfully replaced Commented Jul 24, 2012 at 10:25

4 Answers 4

2

No need to use regex if your replaceCommand array contains defined strings. Use .replace() instead.

Demo

var replaceCommand = ['replace me', 'goodbye'];
var replaceOutput = ['replaced successfully', 'hello'];

$('#text').keyup(function(){
    var text = $('#text').val();
    for (var i=0; i < replaceCommand.length; i++){
        text = text.replace(replaceCommand[i],replaceOutput[i]);
    }
    $('#text').val(text);
});​

Update 1 : I hope you are $('#text') is a text box. So you need to get the value using $('#text').val(). Also .replace() will return the replaced string. So you need to assign again it to the textbox value.

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

1 Comment

okay, so where are you typing in? You can't type into a div. There must be an input element. Can you update the question with the html?
1

Use blur of the text box instead. the below code doesn't use regex though Let me know if the below code helps:

 $('#text').blur(function(){ 
var text = $('#text').text(); 
var replaceCommand = ['replace me', 'goodbye']; 
var replaceOutput = ['replaced successfully', 'hello'];     
if($(this).val() == replaceCommand [0]){
$(this).val(replaceOutput[0]) ;
    }
else if($(this).val() == replaceCommand [1]){
$(this).val(replaceOutput[1]) ;
    }

});  

Working JSFIDDLE

2 Comments

that's an interesting approach but it didn't help
Check the fiddle which i shared.. works fine there....Can you set up a demo on Jsfiddle so that we can have a look?
0

It's as good a way as any, but not sure I would use keydown as it will fire on every keypress. But if you must have it as soon as the last character of the word replace has been entered then key down is the one (or key up).

However, it will not fire if it is pasted in via the mouse. So you will need to also trap mouse events.

If the value in the box is all you need then an onchange will do the trick, but won't fire until you lose focus (different browsers will behave differently).

1 Comment

but won't fire until you lose focus??? that is not right...it fires whenever the state changes.
0

You would try something like this:

$('#text').bind("keydown blur", function(){
                var jText = $(this);
                var newTxt = jText.val().replace(/replace(?!\s*successfully)/gi, "replace successfully");
                jText.val(newTxt);
            });

Example

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.