I’m building a website that uses jQuery for things like Modals and form validation. I find myself able to write the code fairly easily, but it’s not very modular and I find myself re-writing a lot of the code. I have a CS background, but I’m very new to jQuery. I understand that jQuery has Object-Oriented capabilities, but I haven’t quite gotten a grasp on how that is done in jQuery, so my code looks incredibly procedural, and I’d like to be able to modularize it better.
Take something like this for example:
$("#show").click( function() {
$("#view").fadeIn(500);
return false;
});
$("#view .popup-close a").click( function() {
$("#view").fadeOut(500);
return false;
});
I have a list of similar declarations everywhere I have Modals. How would I write this statement in a more modular way?
If you’d like to make this into a reusable plugin throughout your site, that wouldn’t be too difficult.
I’d suggest giving the Plugins/Authoring guideline a read: http://docs.jquery.com/Plugins/Authoring
Here’s a start on how you’d go about pluginifying your popup:
And here’s a working example: http://jsfiddle.net/HMX9J/