423,818 Members | 2,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,818 IT Pros & Developers. It's quick & easy.

How do i consolidate jQuery repetitive code

P: 18
Hi,
I have a series of textbox wired up with jQuery Autocomplete feature, there are 12 textbox in total.

is thr any way to avoid repetitive code,
Any help, i will be very thankful.

Expand|Select|Wrap|Line Numbers
  1. $('#prod_rate_0').val("");
  2.             $('#vat_0').val("");
  3.  
  4.             $('#prod_rate_1').val("");
  5.             $('#vat_1').val("");
  6.  
  7.             $('#prod_rate_2').val("");
  8.             $('#vat_2').val("");
  9.  
  10.             $('#prod_rate_3').val("");
  11.             $('#vat_3').val("");
  12.  
  13.             $('#prod_rate_4').val("");
  14.             $('#vat_4').val("");
  15.  
  16.             $('#prod_rate_5').val("");
  17.             $('#vat_5').val("");
  18.  
  19.             $('#prod_rate_6').val("");
  20.             $('#vat_6').val("");
  21.  
  22.             $('#prod_rate_7').val("");
  23.             $('#vat_7').val("");
  24.  
  25.             $('#prod_rate_8').val("");
  26.             $('#vat_8').val("");
  27.  
  28.             $('#prod_rate_9').val("");
  29.             $('#vat_9').val("");
  30.  
  31.             $('#prod_rate_10').val("");
  32.             $('#vat_10').val("");
  33.  
  34.             $('#prod_rate_11').val("");
  35.             $('#vat_11').val("");
  36.  
  37.             $("#prod_code_0").autocomplete({
  38.                 source: "prod_rpc.php",
  39.                 minLength: 2,
  40.                 select: function(event, ui) {
  41.                     $('#p_id').val(ui.item.p_id);
  42.                     $('#prod_rate_0').val(ui.item.prod_rate);
  43.                     $('#vat_0').val(ui.item.vat);
  44.                 }
  45.             });
  46.  
  47.             $("#prod_code_1").autocomplete({
  48.                 source: "prod_rpc.php",
  49.                 minLength: 2,
  50.                 select: function(event, ui) {
  51.                     $('#p_id').val(ui.item.p_id);
  52.                     $('#prod_rate_1').val(ui.item.prod_rate);
  53.                     $('#vat_1').val(ui.item.vat);
  54.                 }
  55.             });
  56.  
  57.             $("#prod_code_2").autocomplete({
  58.                 source: "prod_rpc.php",
  59.                 minLength: 2,
  60.                 select: function(event, ui) {
  61.                     $('#p_id').val(ui.item.p_id);
  62.                     $('#prod_rate_2').val(ui.item.prod_rate);
  63.                     $('#vat_2').val(ui.item.vat);
  64.                 }
  65.             });
  66.  
  67.             $("#prod_code_3").autocomplete({
  68.                 source: "prod_rpc.php",
  69.                 minLength: 2,
  70.                 select: function(event, ui) {
  71.                     $('#p_id').val(ui.item.p_id);
  72.                     $('#prod_rate_3').val(ui.item.prod_rate);
  73.                     $('#vat_3').val(ui.item.vat);
  74.                 }
  75.             });
  76.  
  77.             $("#prod_code_4").autocomplete({
  78.                 source: "prod_rpc.php",
  79.                 minLength: 2,
  80.                 select: function(event, ui) {
  81.                     $('#p_id').val(ui.item.p_id);
  82.                     $('#prod_rate_4').val(ui.item.prod_rate);
  83.                     $('#vat_4').val(ui.item.vat);
  84.                 }
  85.             });
  86.  
  87.             $("#prod_code_5").autocomplete({
  88.                 source: "prod_rpc.php",
  89.                 minLength: 2,
  90.                 select: function(event, ui) {
  91.                     $('#p_id').val(ui.item.p_id);
  92.                     $('#prod_rate_5').val(ui.item.prod_rate);
  93.                     $('#vat_5').val(ui.item.vat);
  94.                 }
  95.             });
  96.  
  97.             $("#prod_code_6").autocomplete({
  98.                 source: "prod_rpc.php",
  99.                 minLength: 2,
  100.                 select: function(event, ui) {
  101.                     $('#p_id').val(ui.item.p_id);
  102.                     $('#prod_rate_6').val(ui.item.prod_rate);
  103.                     $('#vat_6').val(ui.item.vat);
  104.                 }
  105.             });
  106.  
  107.             $("#prod_code_7").autocomplete({
  108.                 source: "prod_rpc.php",
  109.                 minLength: 2,
  110.                 select: function(event, ui) {
  111.                     $('#p_id').val(ui.item.p_id);
  112.                     $('#prod_rate_7').val(ui.item.prod_rate);
  113.                     $('#vat_7').val(ui.item.vat);
  114.                 }
  115.             });
  116.  
  117.             $("#prod_code_8").autocomplete({
  118.                 source: "prod_rpc.php",
  119.                 minLength: 2,
  120.                 select: function(event, ui) {
  121.                     $('#p_id').val(ui.item.p_id);
  122.                     $('#prod_rate_8').val(ui.item.prod_rate);
  123.                     $('#vat_8').val(ui.item.vat);
  124.                 }
  125.             });
  126.  
  127.             $("#prod_code_9").autocomplete({
  128.                 source: "prod_rpc.php",
  129.                 minLength: 2,
  130.                 select: function(event, ui) {
  131.                     $('#p_id').val(ui.item.p_id);
  132.                     $('#prod_rate_9').val(ui.item.prod_rate);
  133.                     $('#vat_9').val(ui.item.vat);
  134.                 }
  135.             });
  136.  
  137.             $("#prod_code_10").autocomplete({
  138.                 source: "prod_rpc.php",
  139.                 minLength: 2,
  140.                 select: function(event, ui) {
  141.                     $('#p_id').val(ui.item.p_id);
  142.                     $('#prod_rate_10').val(ui.item.prod_rate);
  143.                     $('#vat_10').val(ui.item.vat);
  144.                 }
  145.             });
  146.  
  147.             $("#prod_code_11").autocomplete({
  148.                 source: "prod_rpc.php",
  149.                 minLength: 2,
  150.                 select: function(event, ui) {
  151.                     $('#p_id').val(ui.item.p_id);
  152.                     $('#prod_rate_11').val(ui.item.prod_rate);
  153.                     $('#vat_11').val(ui.item.vat);
  154.                 }
  155.             });
  156.  
Jan 5 '11 #1
Share this Question
Share on Google+
2 Replies


Rabbit
Expert Mod 10K+
P: 12,279
You could use a for loop.
Jan 5 '11 #2

Samishii23
100+
P: 246
Best idea would be make an array of what you want then loop as mentioned above.

However, you could try something cleaner and simpler: jQuery UI AutoComplete.

Just make an array of what suggestions you want ( or dynamically create them ), then just:
Expand|Select|Wrap|Line Numbers
  1.  $("#textBox").autocomplete({source: suggestionsArray});
Jan 6 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.