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
2010-03-20 22:37:47
+1 for ingenuity. The answer matches the vagueness of the question.
Tom
2010-03-20 22:38:45
+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
2010-03-20 22:59:22
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
2010-03-21 01:11:29