0

Hi All,

Relatively new to JS, but something strange is happening.

I have one function:

var commentContent = $('textarea.comment-box#comment');

commentContent.keydown(function(e){
    if((e.keyCode || e.which) == 13 && !e.shiftKey && commentContent.is(":focus"))
    {
        e.preventDefault();
        commentForm.submit();
    }
});

Which works absolutely fine. If the object is focused and I press Enter, the submit function is called.

Going down the line, a completely unrelated function:

var loadComments = $('.btn#load');

loadComments.click(function(e){

    e.preventDefault();

        $.ajax({
            type: 'POST',
            url: '/comments/load',
            data: {'listing_id': listingID, 'comment': comment},
            dataType: 'JSON'
        });
    });

When I press this button, Chrome sends me this error: Uncaught TypeError: Cannot read property 'keyCode' of undefined and references the line if((e.keyCode || e.which) == 13 && !e.shiftKey && commentContent.is(":focus"))

I can't think why/how my second function would be firing the keydown event? Any ideas?

2
Contributors
1
Reply
17
Views
1 Year
Discussion Span
Last Post by hielo
0

Try:

var commentContent = $('textarea.comment-box#comment');
commentContent.keydown(function(e){

    /* see: http://www.quirksmode.org/js/events_properties.html */
    var code;
    if (!e){
        var e = window.event;
    }

    if (e.keyCode){
        code = e.keyCode;
    }
    else if (e.which){
        code = e.which;
    }

    if( code == 13 && !e.shiftKey && commentContent.is(":focus"))
    {
        e.preventDefault();
        commentForm.submit();
    }
});
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.