You are not adding a function to the element, you are adding it to the jQuery wrapper around the element. Each time you pass a selector to jQuery, it will create a new wrapper for the elements found:
$('#myEl'); // gives a jQuery wrapper object $('#myEl'); // creates another jQuery wrapper object
If you save the wrapped element into a variable and use it later, it will be a different story because you are accessing the saved jQuery wrapper object.
var dynamicHtmlForm = $('.dynamicHtmlForm'); dynamicHtmlForm.validate = function() { return true; } $('.dynamicHtmlForm .saveButton').click(function() { if (dynamicHtmlForm.validate()) { return false; } return true; }
You can also add a function directly to an element using
$('.dynamicHtmlForm')[0].validate = function () { return true; }
Or you can look at the jQuery extension correctly spelling the plugin .
Andy e
source share