mirsat mirsat - 7 months ago 12
Javascript Question

Function delete and new function activate

I'll form control with "jquery form validation plugin". I want to "init" section click function will be deleted, when I will become activate new function. Or i want to hear your alternative solutions for this situation.

Can you help me please guys. Thanks.

var FormValidation = function() {
var Profile = function() {
var error2 = $('div#tab_1_1 .alert-danger', $('#form1'));
var success2 = $('div#tab_1_1 .alert-success', $('#form1'));
$('#form1').validate({
errorElement: 'span',
errorClass: 'help-block help-block-error',
focusInvalid: true,
ignore: "",
rules: {
name: {
minlength: 2,
required: true
},
lastname: {
minlength: 2,
required: true
}
}
});
}
var ProfileTwo = function() {
var error2 = $('div#tab_1_2 .alert-danger', $('#form1'));
var success2 = $('div#tab_1_2 .alert-success', $('#form1'));
$('#form1').validate({
errorElement: 'span',
errorClass: 'help-block help-block-error',
focusInvalid: true,
ignore: "",
rules: {
name: {
minlength: 2,
required: true
},
lastname: {
minlength: 2,
required: true
}
}
});
}

var Func = Profile();
return {
init: function() {
$("ul#ProfileTabs li").click(function() {
Func = null;
var DataID = $(this).find("a").data("id");
if (DataID === "tab_1_1") {
Func = Profile();
} else if (DataID === "tab_1_2") {
Func = ProfileTwo();
} else {
Func = null;
}
Func();
});
}
};
}();

jQuery(document).ready(function() {
FormValidation.init();
});

Answer

I would make Profile1 and Profile2 functions return only the validate object.

On tab click I would then call the unbind first

$('#form1').data('validator', null);
$("#form1").unbind('validate');

and then reattach with the new profile

validate_obj = <get PROFILE1 or PROFILE2>
$('#form1').validate( validate_obj );
Comments