Is it better to encapsulate functionality in a jQuery plugin or vanilla JavaScript function?

Viewed 478

Let's say i have some bit of JavaScript which will modify the DOM, perhaps hide/show a form field or something like that and let's assume I want to execute this task on multiple pages, but only once or twice per page.

Is it better to encapsulate this functionality into a jQuery plugin, or a vanilla JavaScript function?

Essentially, is this:

jQuery.fn.toggleFormInput = function() {
    // Stunning JavaScript/jQuery magic here
}

better or worse than this:

function toggleFormInput () {
    // Stunning JavaScript/jQuery magic here
}
2 Answers
Related