A interesting problem came in today, a friend of mine requested to code a text area box such that when you press Enter Key inside it. It adds a new line and add comma to the previous line end.
Example:
Respected Customer (Now if i press enter here)
The above should look like: Respected Customer,
How to achieve this:
Logic Designing:
1. When enter key is press detect it
2. When detected get the whole string
3. Add the comma at the last
4. Now add this whole text to the textarea
Code:
<script>
$(document).ready(
function(){
$('#txtarea').keypress(function(e) {
if(e.keyCode == 13) {
var string = $('#txtarea').val();
string = string + ',';
$('#txtarea').val(string);
}
});
});
</script>
Note: #textarea is the Id of the textarea box.
Hope it helps.
You can demo the above version at: Demo