473,408 Members | 2,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How do i consolidate jQuery repetitive code

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
2 2081
Rabbit
12,516 Expert Mod 8TB
You could use a for loop.
Jan 5 '11 #2
Samishii23
246 100+
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

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

Similar topics

51
by: Mudge | last post by:
Please, someone, tell me why OO in PHP is better than procedural.
1
by: deko | last post by:
I have 3 files I need to write to - and need to make sure I have a lock on each, and take action if I can't get a lock. The code below works, but I know it could be more efficient if I used a...
53
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
6
by: jsceballos | last post by:
Hello. I'm writing a proxy class, i.e: a class whose methods mostly delegate their functionality to other class object. Most of the methods (which are quite a lot) defined in the class would end...
83
by: liketofindoutwhy | last post by:
I am learning more and more Prototype and Script.aculo.us and got the Bungee book... and wonder if I should get some books on jQuery (jQuery in Action, and Learning jQuery) and start learning about...
2
by: thj | last post by:
Hi. I've got this form that I'm trying to validate: <form id="periodForm" action="" method="post"> <p> Periode: <input id="startDate" name="startDate" type="text" size="7" value="<%=...
1
by: spyderfusion02 | last post by:
Hi there, I am trying to get the class names dynamically for the script below. I am using the Pagination plugin. I have different class names that are created through PHP so need to get them using...
3
pradeepjain
by: pradeepjain | last post by:
hii.. i use cope like this <td><a href='/login.php' id="view-user" >Pradeep</a></td> and defauly jquery dialog code <script type="text/javascript"> $(function() {...
2
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me , what is meaning of the below jquery statement $('~ span:first',this).text(); i dont understand meaning of the ~ in jquery selector thanks very much for all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.