0

I have a interesting problem I need to solve. A customer wants to enter a customer id (using a scanner) in a input element and using AJAX, it will fill another input element with the customer name. A new input element should be added and focus set, so the customer can enter another customer id. I have two issues that I need help solving. First, I need to capture the enter sent by the scanner and move to the newly added input element. Second, I need to add events to the new input element to handle the AJAX call and the next enter sent from the scanner. Any help would be greatly appreciated.

Wade

2
  • Scanner = hardware peripheral? So you can't figure out how to get the scanner's code in the html? Commented Sep 17, 2010 at 1:13
  • The scanner is nothing more than a keyboard emulator, but it sends a enter key after it sends the keys for the barcode it scanned. My problem is I need to handle the enter key to trigger other code. Commented Sep 17, 2010 at 1:40

3 Answers 3

1

Here's a fairly simple template you can work from:

<form>
    <fieldset>
        Customer ID: <input name="id" />
        Customer Name: <input name="name" />
    </fieldset>
</form>​​​​​​

with Javascript:

​jQuery(function($) {
    var form = $('form'),
        fieldset; // defined here but set below after we get our event attached

    $('input[name=id]').keyup(function(e) {
        if(e.keyCode === 13) {
            var clone = fieldset.clone(true).appendTo(form);

            clone.children('input[name=id]').focus();

            $.get('getCustomerName.php', {id: this.value}, function(data) {
                clone.children('input[name=name]').val(data);
            });
        }
    });

    fieldset = $('fieldset').clone(true); // clones our fieldset template with events
});​
Sign up to request clarification or add additional context in comments.

1 Comment

How do I implement the function you showed above? I am still new to JQuery, so I pretty much have only ever attached functions with the ready event.
1

@Wade73, adding a new control is the easy bit. You'll need to pick an element to place the control within or to though.

$('#PlaceButtonIn').appendTo("new button html");
$('#PlaceButtonIn').append("new button html");

#PlaceButtonIn can be a div or a td or something useful to place the new control in.

New controls do not have events so use the live commend for this.

$('#PlaceButtonIn').append("<input type='button' class='NewButton'>");

$(document).ready(function(){

  $('.NewButton').live('click', function() { //Do click event here });

});

NewButton class does not need to exist in your CSS. It's a handy way to use the jQuery selector if you don't know control Id's or names.

Hope this is helpful.

1 Comment

Looks like this might be what I am missing.
1

Maybe this can answer your doubt:

When input gets focus

 $('#inputId').focus(function(){

      ///your code here
    });

Or

$('#inputId').keypress(function(e) {
    if(e.keyCode == 13) {
        //your code here
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.