0

I have the following code :

<input type="text" id="productCode" name="productCode"  style="min-height: 42px;" onChange="ajaxrequest_provideProductListOnHit('protected/snippet/snippet_provideProductListOnHit.php', 'ajaxOnProductHit'); return false;"  required="required" autofocus />

The problem is that :

  1. The autofocus is not working, I'm using it in this input box only.
  2. Actually the purpose of this input box is to get the field autofocussed so that a barcode scanner could input the productCode.
  3. Now as you can see, my onChange event handler is not going to work here since the barcode scanner apart from the product code, inputs too.

So I need a solution here which autofocuses and once the barcode scanner inputs value in the field, calls for the mentioned ajax function.

2 Answers2

0

html:

<input type="text" id="productCode" name="productCode"  style="min-height: 42px;" required="required" autofocus />

js:

var pc = document.getElementById('productCode');
pc.onblur = function () {     
  ajaxrequest_provideProductListOnHit(
    'protected/snippet/snippet_provideProductListOnHit.php', 
    'ajaxOnProductHit'
  ); 
}
pc.focus();

i use onblur, because onchange would trigger after EVERY change you make (e.g. typing into the text-field will trigger after every key).

you could also provide some custom-logic, e.g. recognize a certain length

hereandnow78
  • 14,094
  • 8
  • 42
  • 48
  • It isn't working, may be I need some event handler which forces browser to focus on the required input field whenever this input field is loaded.BTW I'm using Latest Chrome browser on Win 8 PC. – kunal chakraverthy Aug 18 '14 at 02:21
  • there even your code should work, so there must be any other problem in your site's code – hereandnow78 Aug 18 '14 at 06:24
0

Yes you where right, the problem was that I was using autofocus on a different preceding form field which made this particular field of this particular form non-autofocus. So I learned that in a page with multiple forms loaded in to the DOM, only the first one with auto-focus will work. Fool of me to think otherwise.