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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -