1

I need to improve my jquery code where I repeat my function 6 times!!! is there away to do a loop to shorten the code ?

(function( jQuery ){
    jQuery.fn.vesta = function(imgN){
        var imgPath = "http://localhost:8080/mhost/media/magentohost/vesta/vesta"
        var currImg = imgPath + imgN + ".png";
        var targetImg = jQuery('.img-browser img');
        jQuery('.img-browser img').attr('src', currImg);
    }
})( jQuery );

jQuery('.vesta1').on('click', function (e) {
    jQuery('.vesta1').vesta(1);
});
jQuery('.vesta2').on('click', function (e) {
    jQuery('.vesta2').vesta(2);
});
jQuery('.vesta3').on('click', function (e) {
    jQuery('.vesta3').vesta(3);
});
jQuery('.vesta4').on('click', function (e) {
    jQuery('.vesta4').vesta(4);
});
jQuery('.vesta5').on('click', function (e) {
    jQuery('.vesta5').vesta(5);
});
jQuery('.vesta6').on('click', function (e) {
    jQuery('.vesta6').vesta(6);
});
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

6 Answers6

4

You can DRY this up by using a common class, and a data attribute to specify the parameter to send to your vesta function:

<div class="vesta" data-vesta="1">1</div>
<div class="vesta" data-vesta="2">2</div>
<div class="vesta" data-vesta="3">2</div>

Then there is no need to loop at all:

$('.vesta').on('click', function (e) {
    $(this).vesta($(this).data('vesta'));
});
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2

Just put it into a for loop, and take advantage of the dynamic nature of JavaScript:

for (var i = 1; i <= 6; i++) {
    $('.vesta' + i).on('click', (function (index) {
        return function (e) {
            $('.vesta' + index).vesta(index);
        };
    })(i));
}
meskobalazs
  • 15,741
  • 2
  • 40
  • 63
2

Use a common class and a data attribute

jQuery('.vesta').on('click', function (e) {
    var elem = $(this);
    elem.vesta(elem.data("ind"));
});

and the HTML

<div class="vesta vesta1" data-ind="1">
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

I suppose you need the this reference along with some hack kind of thing

$('[class*=vespa]').on('click', function(e){
    $(this).vesta(+(this.className.match(/vespa(\d+)/)[1]))
});

Here, we capture elements which have a class that matches at least vespa and then we use some bit of regex to match the digits after vespa and + unary operator changes the String version of numbers into actual numbers.

Amit Joki
  • 58,320
  • 7
  • 77
  • 95
1

It would be quite easy if you can alter the structure of the HTML. You would give all elements the same class, say vesta. But you also give them an attribute, say data-number. For example, like this:

<div class="vesta" data-number="4"></div>

Then, your jQuery code would be as simple as:

$(document).on({
    click: function() {
        var $this = $(this),
            number = +$this.data('number');

        $this.vesta(number);
    }
}, '.vesta');
Dennis
  • 14,210
  • 2
  • 34
  • 54
  • @epascarello thanks, corrected - I forgot how I wanted to call the `on` method while typing my answer! :) – Dennis Jan 26 '15 at 13:20
-1

Edit: I was a bit lazy with explaining the code snippet that I have provided an hour ago, but I am modifying my post now in response to the comments.

This code snippet will allow you to apply listeners from '.vesta1' elements to '.vestaN'

[@Variable]

NumberOfClasses - is the positive integer after 'vesta'. Eg: vesta1 ,vesta2, vesta100 ... etc

var NumberOfClasses=6;
for(var i=1;i<=NumberOfClasses;i++){
    var className = '.vesta'+(i+1);
    jQuery(className ).on('click', function (e) {
        $(this).vesta(i);
    });
}
Ji_in_coding
  • 1,691
  • 1
  • 14
  • 17