Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find input children of form, no matter how many parents they have

Tags:

jquery

forms

How is it possible with jQuery, to find all inputs/textareas there is inside a form? I know you can use .children and then .each, but i'm building a validation class and i dont know if some of the input has a wrapper because it should work for all my forms.

$('input[type=submit]').click(function(){
    $(this).closest('form').children('input,textarea').each(function(){
        // do something with the input/textarea
    )};
});

This works, but only if the inputs is right after the form...

like image 829
Lasse Avatar asked Jan 18 '23 09:01

Lasse


1 Answers

Use .find() to get all descendants which matches a given selector:

$('input[type=submit]').click(function() {
    $(this).closest('form').find('input,textarea').each(function() {
        /* Do something with the input/textarea */
    });      
});
like image 167
Rob W Avatar answered Jan 30 '23 22:01

Rob W