"this[0] is undefined" message using jQuery validate plugin -
i've started use jquery validate plugin. having few issues display of error messages , wanted create test page experiment few things. despite same setup working me yesterday, i'm getting follwing message:
this[0] undefined
looking @ jquery code, fails in following section (specific line highlighted):
valid: function() { if ( $(this[0]).is('form')) { return this.validate().form(); } else { var valid = true; **var validator = $(this[0].form).validate();** this.each(function() { valid &= validator.element(this); }); return valid; } }
from looking @ it, must think validator isn't form, is. don't understand what's going on. code fails when try print out result of valid() method console. here's code far. grateful help.
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <style type="text/css"> th { text-align: center; } .labelcol { width: 60px; } </style> <script type="text/javascript" src="jquery-1.6.1.full.js"></script> <script type="text/javascript" src="jquery.validate.full.js"></script> <script type="text/javascript"> $('#myform').validate({ rules: { thisval: "required" } }); console.info($('#myform').valid()); </script> </head> <body> <form id="myform" name="myform" action="" method="post"> <table> <tr> <th colspan="2">header section</th> </tr> <tr> <td class="labelcol">this title</td> <td class="valuecol"> <input type="text" name="thisval" id="thisval" maxlength="45" size="45" /> </td> </tr> <tr> <td> <input type="submit" value="submit" /> </td> </tr> </table> </form> </body> </html>
your #myform
form not loaded when script executed, $('#myform')
doesn't match anything. wrap script in ready event handler.
<script type="text/javascript"> $(function() { // <-- add $('#myform').validate({ rules: { thisval: "required" } }); console.info($('#myform').valid()); }); </script>
Comments
Post a Comment