Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a plugin's method to dynamically created elements?

I'm using the jquery.maskedinput plugin to create a mask for a phone number field:

jQuery('input[type="tel"]').mask("(999) 999-9999");

Using a modification of Ryan Bates's nested form, I have a form where multiple phone numbers can be added dynamically via an "Add phone number" link. This link creates additional inputs, but the mask functionality is not applied. I am aware of the live() method in jQuery but am not sure if/how it can be used to apply the mask() method. Is there a way to apply this mask to dynamically created inputs?

like image 582
robertwbradford Avatar asked Sep 09 '26 21:09

robertwbradford


1 Answers

To answer my own question... I used the jquery.livequery plugin to do the following:

$('input[type="tel"]').livequery(function() {
  $(this).mask("(999) 999-9?999");
});
like image 147
robertwbradford Avatar answered Sep 12 '26 10:09

robertwbradford