tags:

views:

39

answers:

3

How to check if an html element [textbox, select, radiobutton etc] is in a form via Jquery?

+4  A: 

You can use closest to see if there is an ancestor element of the type form:

obj.closest("form").length == 1
Gumbo
+1 for ingenuity. The answer matches the vagueness of the question.
Tom
+2  A: 

You could also do

 obj.is('form *')

If you want to see if the object be in a particular form, you could do this:'

 obj.is('#formId *')
Pointy
A: 

Try this:

<script type="text/javascript">
    function checkboxInForm( form ){
        return ( form.find(':checkbox').size() ) ? true : false;
    }

    $(document).ready(function() {
        var myForm = $('#myFormId');
        if ( checkboxInForm( myForm ) ){
            // do something
        }
    });
</script>

Create a function for each kind of element you need to find.

Kindred