Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following code does exactly what your code does:</p> <pre><code>var passedValidation = true; $('#fbsignup input').each(function(index) { if ($(this).val().trim() == '') { $('#fbsignup tr').filter('.' + $(this).attr('class').split(' ').slice(0, 1)).find('em').show(); passedValidation = false; } }); if (passedValidation) $('#fbcSignupModal').jqmHide(); return passedValidation; </code></pre> <p>... except for one caveat: it'll only work if the classes "firstName", "lastName", etc... are the FIRST class in the class attributes of your inputs. This limitation, and the convoluted line <code>$('#fbsignup tr').filter('.' + $(this).attr('class').split(' ').slice(0, 1)).find('em').show();</code>, only exists because I don't know the structure of your HTML. The selectors can be a lot cleaner (using <code>.sibling()</code>, <code>.children()</code>, <code>.parent()</code>, etc. if the HTML structure is known.</p> <p>Alternatively, include an array of all the classnames of your inputs:</p> <pre><code>var inputClasses = ['firstName', 'lastName', 'email', 'password', 'screenName']; </code></pre> <p>And iterate through these:</p> <pre><code>var passedValidation = true; $.each(inputClasses, function(index, className) { if ($('#fbsignup').find('input.' + className).val().trim() == '') { $('#fbsignup').find('tr.' + className + ' em').show(); passedValidation = false; } }); if (passedValidation) $('#fbcSignupModal').jqmHide(); return passedValidation; </code></pre> <p>The downside to this is that you'll have to manually update the array if you change/add inputs. Your best bet is probably to modify my first solution using the known structure of your HTML, or even convert classes to IDs.</p>
 

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