Branden Ham Branden Ham - 1 year ago 91
jQuery Question

Jquery, formatting in a textarea

I've been working on an extension at work and the only thing that is escaping me is formatting a textarea based on three inputs that i combine and try to make a new line per enter on the last input. It works for the first two lines then the new lines start adding up going to two lines between submissions then 4 etc.

$("#misc").keyup(function(event) {
if (event.keyCode == 13) {
var newText = $('#medication').val() + " " + $('#diagnosis').val() + " " + $('#misc').val();

$("#form").val($("#form").val() + newText + "\r\n");

nextBox = textboxes[0];

return false;

Any help would be greatly appreciated this is kinda what it looks like but its not working in the jsfiddle for some reason.

Edited to add the jsfiddle for the html/css/javascript

Answer Source

You are binding the #misc's keyup event every time when the .medication's keyup event occurs, so it happens n times when #misc's keyup event occurs. Move the #misc's keyup event outside the wrapping event, and it'll be fixed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download