javascript - Validate multiple selectors on click and unclick -



javascript - Validate multiple selectors on click and unclick -

jquery(function($){ var nickname = $('#nickname'); var email = $('#email'); var email_match = $('#email_match'); var password = $('#password'); var birthday_day = $('#birthday_day'); var birthday_month = $('#birthday_month'); var birthday_year = $('#birthday_year'); var gender_femal = $('#gender_female'); var gender_femal = $('#gender_male'); var error = $('.error'); nickname.blur( function(){ if (nickname.val() < 1) error.html('error') }); nickname.click( function(){ if (nickname.val() > 0) error.html('') }); });

now works nickname selector easiest way check selectors without re-create paste same function.

for example

blur/focus(function(){if (nickname || email || password < 0) error.html('error'); });

while i'm typing question i'm getting thought best way duplicate function because want show error if value empty.

but hear pro's believe there way this.

you can target them in 1 selector, so:

var fields = $('#nickname, #email, #password, ...');

but might easier if can identify them mutual like:

var fields = $('#some-container :input');

...or:

var fields = $('.form-fields');

once have means identified set of elements of interest, may bind event handlers of them @ once:

fields.blur(function() { // 'this' refer dom node blurred, though fields refers of them var field = $(this); if(field.val().length == 0) error.html('error'); });

javascript jquery

Comments

Popular posts from this blog

java - Bypassing "final local variable defined in an enclosing type" -

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -