0

I am loading mobile number field dynamically using jquery ajax.

<form role="form" id="loginFrm"> 
        <div class="form-signup pb40"> 
           <label>Enter Mobile Number</label>
           <input class="form-signup-input validate mobnum required" maxlength="10" type="text"id="icon_prefix">              
        </div>
</form>

Javascript

Then, handling keypress event using on(),

$(document).on('keypress','.mobnum',function (e) {          
  //if the letter is not digit then display error and don't type anything
    if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
      return false;
    }
});

But i am searching the target using 'document'. Is this approach is correct? if not, please suggest a best approach.

vishnu
  • 4,377
  • 15
  • 52
  • 89
  • 1
    Since you are adding elements dynamically using AJAX you have to use event delegation. So this is correct approach – Satpal May 26 '15 at 07:40
  • This entirely depends on the lifetime of the `` element or whether it is programatically removed and replaced in the DOM. If it's there on page creation, you *can* use `$('.mobnum').on(...)`. – Phylogenesis May 26 '15 at 07:41
  • http://learn.jquery.com/events/event-delegation/ as @Satpal explain read on this link more about event delegation. – Nishit Maheta May 26 '15 at 07:41

0 Answers0