Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy am I unable to initializing a jQuery function within a getScript callback?
    primarykey
    data
    text
    <p>My specific question/problem has to do with implementing v1.7 of Cedric Dugas' jQuery validation plugin (<a href="http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/#comments" rel="nofollow">http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/#comments</a>), BUT I believe the issue can easily be generalized to other situations.</p> <p>Typically I load the following script like so:</p> <pre><code>&lt;script src="inc/formValidator/js/jquery.validationEngine.js" type="text/javascript"&gt;&lt;/script&gt; </code></pre> <p>Then to initialize the plugin, I do the following:</p> <pre><code>&lt;script&gt; $(document).ready(function() { $("#myFormID").validationEngine(); }); &lt;/script&gt; </code></pre> <p>I have a form with an id attribute with a value of "myFormID" and the script validates the form fields. This works. </p> <p>However, if I include the two bits of code on every page, but the page does not have a form with an ID of myFormID, then I get a null error. To try to address this problem and make the plugin load only when an appropriate form exists, I tried this:</p> <pre><code>&lt;script&gt; if($('#myFormID').length) { $.getScript('inc/formValidator/js/jquery.validationEngine.js', function() { alert('Load was performed.'); }); }; &lt;/script&gt; </code></pre> <p>That works fine too. However, now I can't figure out how to initialize the validationEngine and bind it to the form. Why doesn't this work?</p> <pre><code> $.getScript('inc/formValidator/js/jquery.validationEngine.js', function() { $("#myFormID").validationEngine(); }); </code></pre> <p>I also tried this:</p> <pre><code> $.getScript('inc/formValidator/js/jquery.validationEngine.js', function() { $("#myFormID").load(function() { $(this).validationEngine(); }); }); </code></pre> <p>No luck. I get this error: $("#myFormID") is null.</p> <p>Ideas? I'm nearly bald as it is. This isn't helping!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload