I am trying to automatically do some form preprocessing whenever a form gets loaded on my website. This can happen via a normal page load, but also via an AJAX inline page load. So putting it in $(document).ready(function() { /* ... */ });
does not suit my needs.
What I have so far:
$("form").each(function() {
$(this).ready(function() {
$(this).find(".input input").each(function() {
var required = $(this).data("required");
var checkField = $(this).closest("tr").children(".check");
var errorField = $(this).closest("tr").children(".errormessage");
if (required) {
$(checkField).css("color", "#FFFF00");
$(checkField).html("✘");
$(errorField).css("color", "#FFFF00");
$(errorField).html("(Required)");
}
else {
$(checkField).css("color", "#FFFF00");
$(checkField).html("✔");
$(errorField).css("color", "#000000");
$(errorField).html("");
}
});
});
});
When putting it in $(document).ready(function() { /* ... */ });
it works, but only for the first load. When I'm putting it outside that function, then it never works, it does not even reach the $("form").each(function() { /* ... */ });
.
How to fix this?