Branden Ham Branden Ham - 1 month ago 6
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) {
event.preventDefault();
var newText = $('#medication').val() + " " + $('#diagnosis').val() + " " + $('#misc').val();

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

nextBox = textboxes[0];
nextBox.focus();
nextBox.select();
console.log("first");
$('#medication').val("");
$('#diagnosis').val("");
$('#misc').val("");

return false;
}
});


Any help would be greatly appreciated
https://jsfiddle.net/rcu8dmon/ 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

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.