How to condense this jquery code? -
looking shorten code block:
edit: portion of "list" (3 shown here) actual code has 6.
<script> $('.clickme-newyork, #link4, #link8, #link9').hover(function() { $(".card-cp").addclass("card-bg-cp"); $(".card-gv").addclass("card-bg-gv"); $(".card-hcm").addclass("card-bg-hcm"); }); </script>
this did not work correctly:
<script> $('.clickme-newyork, #link4, #link8, #link9').hover(function() { $(".card-cp, .card-gv, .card-hcm").addclass("card-bg-cp, card-bg-gv, card-bg-hcm"); }); </script>
extrapolating comments, need decide whether "condensing" code necessary. @mojtaba said, can write function work programatically. instance:
function myaddclass(target, classname) { $(target).addclass(classname); }
then do...
<script> $('.clickme-newyork, #link4, #link8, #link9').hover(function() { myaddclass(".card-cp","card-bg-cp"); .... }); </script>
not 'shortened', it?
however if have lot of these do, can put them array , loop through them myaddclass() function. might save space - real question this: shortcut make code shorter (or easier read)? speaking, jquery library provides great shortcuts otherwise verbose code blocks. maybe best bet leave code is.
Comments
Post a Comment