I am working with a CMS system and have restrictions on where I can place code (at the moment). The script I'm trying to use must load after a form and it works successfully in Dreamweaver. But now that I'm moving things into the CMS I must run the script in the header instead of after the form. I'm attempting to load it using after the page loads like so:
<script>
$(document).ready(function() {
//You should create the validator only after the definition of the HTML form
var frmvalidator = new Validator("leadform");
frmvalidator.EnableOnPageErrorDisplaySingleBox();
frmvalidator.EnableMsgsTogether();
frmvalidator.addValidation("first_name","req","Please enter your first name.");
frmvalidator.addValidation("first_name","maxlen=40","Max length for first name is 40.");
frmvalidator.addValidation("first_name","alpha","Please enter your first name.");
frmvalidator.addValidation("last_name","req","Please enter your last name.");
frmvalidator.addValidation("last_name","maxlen=40","Max length for last name is 40.");
frmvalidator.addValidation("last_name","alpha","Please enter your last name.");
frmvalidator.addValidation("email","req","Please enter your e-mail address.");
frmvalidator.addValidation("email","email","Please enter your e-mail address.");
frmvalidator.addValidation("state","dontselect=0","Please select your state.");
});
</script>
I have to believe that I'm just writing this all wrong but I can't find any examples that will help me do this. Clearly I'm a noob coder - any help would be appreciated.
Thanks.
-
To clarify - I need the above script to load so that it comes into effect after the form loads in the browser.Robert Floyd– Robert Floyd2013年04月16日 16:22:55 +00:00Commented Apr 16, 2013 at 16:22
-
2If you are using .ready() it will load after the form loadsdoitlikejustin– doitlikejustin2013年04月16日 16:23:27 +00:00Commented Apr 16, 2013 at 16:23
-
1Are you getting any error messages in the console? When you say this isn't working, how so?colestrode– colestrode2013年04月16日 16:24:44 +00:00Commented Apr 16, 2013 at 16:24
-
colestrode is right. Are you using Firebug or any other development console?orb– orb2013年04月16日 17:38:19 +00:00Commented Apr 16, 2013 at 17:38
-
What CMS are you using Robert?orb– orb2013年04月16日 17:41:26 +00:00Commented Apr 16, 2013 at 17:41
1 Answer 1
$(document).ready(function(event){
// insert code here
});
The above code will always wait until the document is loaded (which means all html including your form is loaded into the page) no matter whether it is in the head or in the body of your html. You just need to make sure that when you are using jQuery you put the script tag with your jQuery code after the script tag that loads jQuery. Here is an example that loads version 1.9.1 of jQuery:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<!-- make sure your code is added after jQuery -->
<script src='pathtoyourscript/yourScript.js'></script>