Los Techies : Blogs about software and anything tech!

A Lesson in DRY Learned with jQuery


While working on a credit card entry form I decided to add a dynamic block of XHTML to highlight the credit card that is entered by the user, while graying out others. In this code, I attempt to retrieve an image object in the DOM using simple string comparison based on the input of a text field.

    var $card = getCardByNumber($(this).val());
    if($card) {
        lowlightCards();
        highlightCard($card);
    } else {
        highlightCards();
    }

If nothing is found, we ensure that all the cards are highlighted. If a card object is found, gray them all out, but highlight the “found” card.

all_cards_on 
The display when no numbers match the card type pattern.

visa_card_on 
The display when a number matches the Visa card type pattern.

The two functions that gray out all the cards, or subsequently highlight all the cards, originally looked like this. They were very similar and “Don’t Repeat Yourself” (DRY) was screaming out loud. Time to refactor!

    function highlightCards() {
        $(".creditcardicon[src^='/Content/images/']").each(function() {
            highlightCard($(this));
        });
    }
    function lowlightCards() { 
        $(".creditcardicon[src^='/Content/images/']").each(function() {
            lowlightCard($(this));
        });
    }

These worked and I could have left them alone, but a lot of my recent work in C# of passing around Funcs and Actions made it clear to me that there was only one difference here. A difference that JavaScript is totally able to remedy. I’ll just pass into a single function, the difference, which is another function. Good ole’ JavaScript!

    function eachCard(action) {
        $(".creditcardicon[src^='/Content/images/']").each(function() { action($(this)); });
    }
Kick It on DotNetKicks.com
Posted Jan 29 2009, 12:55 PM by Chris Missal
Filed under: ,

Comments

DotNetShoutout wrote A Lesson in DRY Learned with jQuery - Chris Missal
on 01-29-2009 2:37 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

heyhey wrote re: A Lesson in DRY Learned with jQuery
on 01-29-2009 2:47 PM

so you work at paypal? eh?

Chris Missal wrote re: A Lesson in DRY Learned with jQuery
on 01-29-2009 2:59 PM

@heyhey No actually, what makes you think that?

Jonathan wrote re: A Lesson in DRY Learned with jQuery
on 01-30-2009 7:34 AM

At first glance, your article scared me!  When I saw the credit card logos, I instantly thought LosTechies was going to start charging--like Experts Exchange.  Thank goodness you're not!

Dew Drop - January 30, 2009 | Alvin Ashcraft's Morning Dew wrote Dew Drop - January 30, 2009 | Alvin Ashcraft's Morning Dew
on 01-30-2009 10:46 AM

Pingback from  Dew Drop - January 30, 2009 | Alvin Ashcraft's Morning Dew

conan wrote re: A Lesson in DRY Learned with jQuery
on 01-30-2009 2:24 PM

Nice example of how function references can so nicely kill code duplication ... unfortunately, either java will never have them, or it will but the syntax will be horrendous ...

Arjan`s World » LINKBLOG for January 30, 2009 wrote Arjan`s World » LINKBLOG for January 30, 2009
on 01-30-2009 2:43 PM

Pingback from  Arjan`s World    » LINKBLOG for January 30, 2009

Tuna Toksoz wrote re: A Lesson in DRY Learned with jQuery
on 02-08-2009 5:57 AM

@Chris Missal Paypal has the same feature(highlighting a credit card, while you're typing) so i guess that is the reason :)

Add a Comment

(required)  
(optional)
(required)  
Remember Me?

Enter the numbers above:
Copyright Los Techies 2008, 2009. All rights reserved.
Powered by Community Server (Commercial Edition), by Telligent Systems