Take the 2-minute tour ×

I have two textboxes and one Button control.....In first TextBox when press enter key moves to next textbox(Barcode) and when i press enter in barcode textbox it fires the button click event......till that its ok....

But what happening after fireing the Button click even on enter in Barcode Textbox its going back to focus on first textbox.........But i want this to stay in same Barcode TextBox to scan more barcodes.

The code is below.

 <script type="text/javascript" 

<script type="text/javascript">
        $(function() {
            var $inp = $('input:text');
            $inp.bind('keydown', function(e) {
                //var key = (e.keyCode ? e.keyCode : e.charCode);
                var key = e.which;
                if (key == 13) {
                    var nxtIdx = $inp.index(this) + 1;
                    $(":input:text:eq(" + nxtIdx + ")").focus();

share|improve this question
add comment

2 Answers

You're handling keydown for every textbox.

You need to modify your selector to only handle keydown for the first textbox.

share|improve this answer
But How can u write code here –  Malik Usman Jan 5 '11 at 17:59
I have no idea what you're asking. –  SLaks Jan 5 '11 at 18:03
The same way you only fire the focus event on the first text input on line 2 of the JavaScript, I would imagine. –  adamjford Jan 5 '11 at 18:03
I don't JavaScript i just got this code from Web i am beginner that's why i am asking.....if you can correct this code. i will appriciate –  Malik Usman Jan 5 '11 at 18:06
add comment

The button click is causing a postback to the server and reloading the page, which re-executes the javascript you posted including this line:


which sets focus on the first text input.

I can't offer much of a solution without the HTML, but hopefully understanding the cause can point you in the right direction.

share|improve this answer
add comment

Your Answer


