Branden Ham
Branden Ham

Reputation: 92

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

Upvotes: 0

Views: 248

Answers (2)

Also your medication class in keyup event is triggered in the last imput too. You try to fix this, validating the last imput, but you compare a type "undefined" with null. An option it's simply remove "!== null": http://www.w3schools.com/js/js_comparisons.asp

if (textboxes[currentBoxNumber + 1])

Upvotes: 0

Taha Paksu
Taha Paksu

Reputation: 15616

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.

Upvotes: 1

Related Questions