Wednesday, July 7, 2010

DRY programming with jquery

Programmer Question

Posting as a jquery newbie. I am sure there must be a way to condense the code below in a DRY fashion. Essentially this is just a show/hide that is applied to multiple elements on a page, all using the same template and naming conventions:



$("#homelink1").hover(
function() { $("#poptext1").show(); },
function() { $("#poptext1").hide(); }
);

$("#homelink2").hover(
function() { $("#poptext2").show(); },
function() { $("#poptext2").hide(); }
);
...


I'm stuck on how to convert this to a function with passed parameters, so that I could just pass the integer (1 or 2) and have the function evaluate the rest, e.g.



$("#homelink" + param).hover



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails