user3100350
user3100350

Reputation: 13

Jquery each on a specific form elements only

I am trying to make my own form validator

At first I was using this code

    $(".check_submit").submit(function(event){

            event.preventDefault();

            var error = false;

            $(".req_field").each(function( index ){

                    if(!$(this).val()){                
                        error = true;
                    }                                    

            });

            if(error == false){
                this.submit();                    
            }else{
                alert("fill all please");
            }                

    });

then i found that if i have more than one form in the same page conflicts happen,

So I wrote this code but there is some thing wrong

    $(".check_submit").submit(function(event){

            event.preventDefault();

            var error = false;

            $(this).children(".req_field").each(function( index ){

                    if(!$(this).val()){                
                        error = true;
                    }                                    

            });

            if(error == false){
                this.submit();                    
            }else{
                alert("fill all please");
            }                

    });

Upvotes: 1

Views: 45

Answers (2)

dkellner
dkellner

Reputation: 9986

You need "find" instead of "children". "Children" only selects the immediate descendants.

Upvotes: 2

A. Wolff
A. Wolff

Reputation: 74410

Use find() instead:

$(this).find(".req_field").each(...)

children() will look only for first level descendants

Upvotes: 3

Related Questions