javascriptjquerytextareabootbox

Get value from textarea in bootbox


I have a bootbox.dialog with a textarea. I want to save the value of the textarea with rowbrakes in the same place as it was written to the database. So I need to create a string with /n or <br>.

Right now is my value undefined.

Here is my code:

$('#btnComment').click(function () {        
    var comment = $("#lblComment1").html();
    popup.dialog({
        title: translator.get('EditComment'),

        message: "<textarea cols='60' rows='6' name='editComment'>" + comment + "</textarea>",

        buttons: {
            confirm: {
                label: translator.get('EditComment'),
                className: "btn-success",

                callback: function (result) {
                    if (result === null) {

                    } else {
                        $("#editComment").val(result);
                        $("#lblComment1").html(result);                            

                        var ajaxData = {
                            Type: "Comment",
                            OrderId: $("#lblOrder").html(),
                            newValue: $("#editComment").val(),
                            MiddocID: $("#hidMiddocID").val()
                        }

                        $.ajax({
                            type: 'post',
                            url: configuration.baseUrl + '/api/OrdersPostback', //
                            dataType: "json",
                            data: JSON.stringify(ajaxData),
                            contentType: "application/json; charset=utf-8"
                        }).then(function (bResult) {
                            if (bResult) {
                                $("#editComment").val(result);
                                $("#lblComment1").html(result);
                            }
                        });
                        //$("#hidBtnComment").click();
                    }
                }
            },

            cancel: {
                label: translator.get('Cancel'),
                className: "btn-default"
            }
        },


    })

});

Any suggestions of what I can do?


Solution

  • Change your <textarea> into this:

    message: "<textarea id='editComment' cols='60' rows='6' name='editComment'>" + comment + "</textarea>",
    

    Note that I've added the id="editComment" which you didn't do in the first place. Now that element will be fetchable by $('#editComment') and you can use .val() and other jQuery methods/plugins.