Uncaught SyntaxError: Invalid or unexpected token “\n” in Javascript

Hi, I am trying to implement a function to copy data from excel & paste it into multiple text fields at my webpage. I found this solution, but I get this error due to “\n” is there any solution for this? I have already tried with “\n” and “\r\n” both not working. I’m currently implementing this to an AdonisJS project which using “edge” template

$('input').bind('paste', function (e) {
    var $start = $(this);
    var source

    //check for access to clipboard from window or event
    if (window.clipboardData !== undefined) {
        source = window.clipboardData
    } else {
        source = e.originalEvent.clipboardData;
    }
    var data = source.getData("Text");
    if (data.length > 0) {
        if (data.indexOf("\t") > -1) {
            var columns = data.split("\n");
            $.each(columns, function () {
                var values = this.split("\t");
                $.each(values, function () {
                    $start.val(this);
                    if ($start.closest('td').next('td').find('input,textarea')[0] != undefined || $start.closest('td').next('td').find('textarea')[0] != undefined) {
                    $start = $start.closest('td').next('td').find('input,textarea');
                    }
                    else
                    {
                     return false;  
                    }
                });
                $start = $start.closest('td').parent().next('tr').children('td:first').find('input,textarea');
            });
            e.preventDefault();
        }
    }
});































(function ($) {

    $.fn.enableCellNavigation = function () {

        var arrow = {
            left: 37,
            up: 38,
            right: 39,
            down: 40,
            enter: 13
        };

        // select all on focus
        this.find('input,select').keydown(function (e) {
            // shortcut for key other than arrow keys
            if ($.inArray(e.which, [arrow.left, arrow.up, arrow.right, arrow.down, arrow.enter]) < 0) {
                return;
            }

            var input = e.target;
            var td = $(e.target).closest('td');
            var moveTo = null;

            switch (e.which) {

                case arrow.left:
                    {
                        if (typeof input.selectionStart == 'undefined') {
                            moveTo = td.prev('td:has(input,select)');
                        } else if (input.selectionStart == 0) {
                            moveTo = td.prev('td:has(input,select)');
                        }
                        break;
                    }
                case arrow.right:
                    {
                        if (typeof input.selectionStart == 'undefined') {
                            moveTo = td.next('td:has(input,select)');
                        } else if (input.selectionEnd == input.value.length) {
                            moveTo = td.next('td:has(input,select)');
                        }
                        break;
                    }
                case arrow.enter:
                    {

                        var tr = td.closest('tr');
                        var pos = td[0].cellIndex;

                        var moveToRow = null;
                        if (e.which == arrow.down) {
                            moveToRow = tr.next('tr');
                        } else if (e.which == arrow.up) {
                            moveToRow = tr.prev('tr');
                        }

                        if (moveToRow.length) {
                            moveTo = $(moveToRow[0].cells[pos]);
                        }

                        break;
                    }

                case arrow.up:
                case arrow.down:
                    {

                        var tr = td.closest('tr');
                        var pos = td[0].cellIndex;

                        var moveToRow = null;
                        if (e.which == arrow.down) {
                            moveToRow = tr.next('tr');
                        } else if (e.which == arrow.up) {
                            moveToRow = tr.prev('tr');
                        }

                        if (moveToRow.length) {
                            moveTo = $(moveToRow[0].cells[pos]);
                        }

                        break;
                    }

            }

            if (moveTo && moveTo.length) {

                e.preventDefault();

                moveTo.find('input,select').each(function (i, input) {
                    input.focus();
                    input.select();
                });

            }

        });

    };

})(jQuery);
$(function () {
    $('table').enableCellNavigation();
});

If you look at MDN Paste they use this

  target.addEventListener('paste', (event) => {
    let paste = (event.clipboardData || window.clipboardData).getData('text');
   ...
});``

So try substituting

if (window.clipboardData !== undefined) {
    source = window.clipboardData
} else {
    source = e.originalEvent.clipboardData;
}
var data = source.getData("Text");

for

let data = (event.clipboardData || window.clipboardData).getData('text')

I think you may also need to escape the newline and tab characters
e.g.

var columns = data.split("\\n");

Not… quite sure I agree with your definition of columns there, by the by… wouldnt \n’s be the rows?

Other thoughts:

  • Why do we care if there’s an indexof tab?
  • $start is defined as $(this), but if you’re not pasting in the first row, first column, things aren’t going to go right. Probably better to define $start as $(this).closest("table").children("tr:first").children("td:first").find("input,textarea");, and that way your pasting works in any cell of the table…
  • Unexpected token usually means something went wrong syntactically. If you’ve copy and pasted this code, make sure that the quote marks aren’t some form of special quote mark (Macs especially love replacing straight quotes with ‘curly quotes’)

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.