javascript - Can the following code be shortened into a "ForLoop"? -
is possible shorten following code "for loop" or of sort? appreciated.
$('#submit').click(function(){ var baronevalue = $('.barone-value').val(); var bartwovalue = $('.bartwo-value').val(); var barthreevalue = $('.barthree-value').val(); var barfourvalue = $('.barfour-value').val(); var barfivevalue = $('.barfive-value').val(); $('.barone').attr('percentvalue', baronevalue); $('.bartwo').attr('percentvalue', bartwovalue); $('.barthree').attr('percentvalue', barthreevalue); $('.barfour').attr('percentvalue', barfourvalue); $('.barfive').attr('percentvalue', barfivevalue); });
as mentioned in comments, can make function help:
function int2word(i){ var map = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]; return map[i]; }
then make loop:
$('#submit').click(function(){ for(var = 1; < 6; i++){ $('.bar' + int2word(i)).attr('percentvalue', $('.bar' + int2word(i) + '-value').val()); } });
Comments
Post a Comment