- You want to look for keyup, not keypress (you want to make sure you get the whole string.
- You are trying to put the textbox value right? You're looking for the textarea value in line two of the javascript.
- If you replace sample on the first key stroke, there won't be anything to replace the second key stroke.
- You can simplify lines 3 and 4 into one line.
- replace can only be used on a string. So you need to get the value first, if you're going to do it that way. txtAreaValue.val().replace('sample', $(this).val());
Feel free to play around with it on this fiddle: http://jsfiddle.net/snlacks/abc6skp9/
$("#txtBox").on('keyup', function () { var txtValue = $(this).val(); $('#txtArea').val("this is a " + txtValue); });
If you have a longer string, replace might work better, but you still need to store the full string somewhere.
var longString = "some really long string... sample... more..."; $("#txtBox").on('keyup', function () { var txtValue = $(this).val(); $('#txtArea').val(longString.replace('sample', txtValue); });
$(#txtArea).val();