I have a jQuery code that handles all validations in my page and its working perfect.
Today I try to dynamic create a form (jQuery append form) but its not working with the new form, which is correct because when the page is loaded the form was not there. So far so good.
I'm trying to modify my code to work with this new form but I'm not sure how to do it and I need some help.
Here is the code:
if($.isFunction($.fn.validate))
{
$("form.validate").each(function(i, el)
{
var $this = $(el),
opts = {
ignore :[],
rules: {},
messages: {},
errorElement: 'span',
errorClass: 'validate-has-error',
highlight: function (element) {
$(element).closest('.form-group').addClass('validate-has-error');
},
unhighlight: function (element) {
$(element).closest('.form-group').removeClass('validate-has-error');
},
errorPlacement: function (error, element)
{
},
submitHandler: function (form) {
$(form).ajaxSubmit({
dataType: 'json',
error: function() {
},
success: function(j) {
if(j.success)
{
//handle success here
}
else
{
//handle error here
}
}
})
return false;
}
}
);
$this.validate(opts);
});
}
So for new forms I have to run the code again because I use foreach form that has class validate.
I try to use the live feature of jQuery (actually is .on() now) and I have not find how to implement yet.
Of course any other ideas or suggestions are welcome.
Thanks.