Shariful_Islam
Shariful_Islam

Reputation: 361

Keypress evnt() is not firing in Firefox but in Chrome

I want to post some data to controller using ajax, when someone press enter a textbox. My code working fine on Chrome but not in firefox. I have tried several solution but it did not work for me.Here is my code:

<input type="text"   id="[email protected]" onkeypress="Reply(this)"/>

and

<script>

function Reply(e){
    var id;
    id = e.id;


        var keycode = (event.keyCode ? event.keyCode : event.which);
        if (event.keyCode == '13') {
            //var txt1 = "\"";
            //var txt2 = txt1.concat(id);
            //var txt3 = "\"";
            //var ActivityId = txt2.concat(txt3);
            var storyActivityId = id.replace("txtbox_", "");
            var liId = '#' + "liPartial_" + storyActivityId;
            var txtId='#'+id;
            //event.stopPropagation();

            $.ajax({

                url: '@Url.Action("PostReply", "Feed")',
               type: 'post',
               cache: false,
               async: true,
               InsertionMode: 'InsertionMode.InsertAfter',
               data: { id:e.id,status:$(txtId).val()},
               success: function (data) {
                   $(liId).append("<br>" + data);
                   $(txtId).val('');
               }
           })
    }
}

Upvotes: 0

Views: 100

Answers (2)

Jai
Jai

Reputation: 74738

Change this:

onkeypress="Reply(this)"

To this:

onkeypress="Reply(event, this)"

Pass event as a first arguement.

Upvotes: 0

Anoop Joshi P
Anoop Joshi P

Reputation: 25537

You need to pass the event to that function,

<input type="text"   id="[email protected]" onkeypress="Reply(event)"/>

Script

function Reply(event) {
    var id;
    id = this.id;
    var keycode = (event.keyCode ? event.keyCode : event.which);
    alert(keycode);
    if (event.keyCode == '13') {
        //var txt1 = "\"";
        //var txt2 = txt1.concat(id);
        //var txt3 = "\"";
        //var ActivityId = txt2.concat(txt3);
        var storyActivityId = id.replace("txtbox_", "");
        var liId = '#' + "liPartial_" + storyActivityId;
        var txtId = '#' + id;
        //event.stopPropagation();

        $.ajax({

            url: '@Url.Action("PostReply", "Feed")',
            type: 'post',
            cache: false,
            async: true,
            InsertionMode: 'InsertionMode.InsertAfter',
            data: {
                id: e.id,
                status: $(txtId).val()
            },
            success: function (data) {
                $(liId).append("<br>" + data);
                $(txtId).val('');
            }
        })
    }
}

Demo

Upvotes: 1

Related Questions