danny danny - 1 year ago 72
jQuery Question

Forms all inputs access in jquery plugin

I am trying to make jquery plugin for form validation (for learning). In this form i am unable to get all form fields which has class name "required". The code is following:

(function( $ ) {

$.fn.kValidation = function() {

this.on("submit", function (event ) {


$(this + " .required").each( function () {

\\this loop dont get all form inputs which has class "required"


}( jQuery ));

HTML is following:

<form id="fo">
<input class="required" data-kvLimit="min:5 max:200" data-kvType="numeric" /><br>
<input class="required" data-kvLimit="min:5 max:200" data-kvType="string" /><br>
<input class="required" data-kvType="email" /><br>
<input type="submit" value="Submit" />

I am calling plugin this way


Answer Source

You can set context parameter of jQuery() to this : form element. Note also, missing closing }) at .on(), and closing } at $.fn.kValidation

  $(function() {
    (function($) {
      $.fn.kValidation = function() {
        this.on("submit", function(event) {
          $(" .required", this).each(function() {



plnkr http://plnkr.co/edit/DqX44H5p9HEA3P5tfZMC?p=preview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download