Can jQuery add a plugin function to future members of a .class?
Here's something I don't know if it's possible, but here's my question:
My jQuery application has dynamically added entry fields. For example, a button "Add New Customer" adds a new entry form after the current one. So far, so good!
However, I find myself needing to add the mask functions I want to every newly created field, like this:
$("#customer_zipcode\\["+customerCount+"\\]).zipcodeMask(....);
zipcodeMask(...)
is just an example function that checks to make sure the user's entered a correct zip code. Other similar masks for phone numbers, credit cards, etc. This part works great, but I have to set it for every newly created element I want to have a zip code mask.
I thought it would be awesome if there was something like .live()
that could do the .zipcodeMask()
call for every future instance of the zip code field.
I thought I'd make a class, such as "UseZipCodeMask"
, and do something like class="UseZipCodeMask"
in every input field that needs a zip code mask.
When I do this:
$(".UseZipCodeMask").zipcodeMask(....);
This works fine - for every existing element that has this class. If I create a new field with this class, it's doesn't have the .zipcodeMask(...)
function working.
Is there a way to make my zip code fields get .zipcodeMask(...)
automatically set for it? It would clean up the code a bit, there's a lot of code that adds .zipcodeMask()
and other related functions every time some new开发者_JAVA百科 input fields are created.
Thanks very much!
What jquery version are you using? You can use .delegate which means you would not have to "add the plugin" to newly created elements.
精彩评论