jQuery: Selecting an element by a variable with it's name
<p>It's about a validation resting upon some ajax and jQuery:
I've got a problem with giving out the data like I wish - resp. to hide it, if the validation-error exists no more, because the user corrected it.
The id of the field, that is requested is loaded in the variable <em>field</em>, the id of the div-tag i want the error-message loaded in should depends on this fieldname, so that I can use the function dynamicly for all input-fields I want to validate. 
So far the idea behind this, here's the code of the success-method of the ajax-request:</p>

<pre><code>function(data) { 
    var error = '#' + field + '-error';

    if($(error).lenght == 0 AND data.lenght != 0) {
        $('#' + field).after($('&lt;div id="' + error + '" class="error-message"&gt;' + data + '&lt;/div&gt;'));
    } 
    else {
        $(error).remove();
    }
}
</code></pre>

<p>The function should easily check whether or not an error-message is given or set.
Unfortunately jQuery seems not to be able to select the div-tags by the id in the variable. The output of error and <em>field</em> is correct, but $(error).lenght returnes <em>undefined</em> </p>
