6

I have written the following recursive input validator and it works for me. Is there a better way to visit each dom element and check if it is an input field and validate it?

function formValidator(parent)
{
    //base case no children
    if( parent.children().length == 0)
        return

    //recurse through each childs' child
    parent.children().each(function(){
        formValidator($(this));

    /**
    * Work : check if this node is an input node
    */
        if($(this).is("input"))
        {
            var type = $(this).attr('type');

            if(type =="text")
              //do work bro

        }

   });//end for each

}
3
  • 2
    This question might be better suited for the code review stack exchange since the nature of your question is to seek improvement rather than help with a problem. Commented May 20, 2015 at 20:27
  • Oh my, I didn't even know code review was a thing. Can I move my post there or can someone move it for me? Commented May 20, 2015 at 20:27
  • You can delete your post here and repost it there (preferred), or flag it for a moderator's attention, asking for it to be migrated (probably avoid that as it's more work for the overworked moderators). — now you have an answer I don't think you can delete it. Commented May 20, 2015 at 20:30

2 Answers 2

4

If by better you mean less verbose, this is equivalent functionally

parent.find('*').each(function(){
    /**
    * Work : check if this node is an input node
    */
        if($(this).is("input"))
        {
            var type = $(this).attr('type');

            if(type =="text")
              //do work bro

        }

   });//end for each

Notice that there is no need for recursion here because

parent.find('*')

uses the *(all-selector). This will get all children and nested children.

Update

To improve performance, you can refactor above to

parent.find('input[type="text"]').each(function(){
        var type = $(this).attr('type');

                //if(type =="text")
                  //above no longer needed
       });//end for each

This will get all nested input elements so you won't have to even check for

if($(this).is("input"))
Sign up to request clarification or add additional context in comments.

3 Comments

I like this answer but I'm mildly curious which would be more efficient.
You can avoid the type check with parent.find('input:text') ...
@RickHitchcock, very nice observation! Updated my answer. Thanks!
3

I would use a narrower selector:

parent.children().find("input:text").each(function(){
    // Valid $(this) element to validate
});

1 Comment

This excludes inputs that are immediate children of parent, which the OP's code handles (if($(this).is("input"))). You can fix it by removing the children() method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.