Getting an assigned jQuery variable to "re-query"
<p>I'm trying to use jQuery to build a home-made validator.  I think I found a limitation in jQuery: When assigning a jQuery value to a json variable, then using jQuery to add more DOM elements to the current page that fit the variable's query, there doesn't seem to be a way to access those DOM elements added to the page which fit the json variable's query.</p>

<p>Please consider the following code:</p>

<pre><code>var add_form = {
    $name_label: $("#add-form Label[for='Name']"),
    $name: $("#add-form #Name"),
    $description_label: $("#add-form Label[for='Description']"),
    $description: $("#add-form #Description"),
    $submit_button: $("#add-form input#Add"),
    $errors: $("#add-form .error"),
    error_marker: "&lt;span class='error'&gt;&amp;nbsp;*&lt;/span&gt;"
}
function ValidateForm() {
    var isValid = true;

    add_form.$errors.remove();

    if (add_form.$name.val().length &lt; 1 ) {
        add_form.$name_label.after(add_form.error_marker);
        isValid = false;
    }
    if (add_form.$description.val().length &lt; 1) {
        add_form.$description_label.after(add_form.error_marker);
        isValid = false;
    }

    return isValid
}
$(function(){
    add_form.$submit_button.live("click", function(e){
        e.preventDefault();
        if(ValidateForm())
        {
        //ajax form submission...
        }
    });
})
</code></pre>

<p>An example is availible here: <a href="http://jsfiddle.net/Macxj/3/" rel="nofollow">http://jsfiddle.net/Macxj/3/</a></p>

<p>First, I make a json variable to represent the html add form.  Then, I make a function to validate the form.  Last, I bind the click event of the form's submit button to validating the form.</p>

<p>Notice that I'm using the jQuery after() method to put a span containing an '*' after every invalid field label in the form.  Also notice that I'm clearing the asterisks of the previous submission attempt from the form before re-validating it (this is what fails).</p>

<p>Apparently, the call to add_form.$errors.remove(); doesn't work because the $errors variable only points to the DOM elements that matched its query when it was created. At that point in time, none of the labels were suffixed with error_marker variable.</p>

<p>Thus, the jQuery variable doesn't recognize the matching elements of it's query when trying to remove them because they didn't exist when the variable was first assigned.  It would be nice if a jQuery variable HAD AN <strong>eval()</strong> METHOD that would re-evaluate its containing query to see if any new DOM elements matched it.  But alas...</p>

<p>Any suggestions?</p>

<p>Thanks in advance!</p>
