Caling AJAX Method for multiple hidden ids aspx I am calling a ajax method for a number of hidden client ids on an aspx page.  The ajax method returns some html to a div provided.

So the code looks like below:

&lt;asp:HiddenField runat="server" ID="hdnSomeProperty" Value="False"/&gt; 
&lt;asp:HiddenField runat="server" ID="hfMyIndex" Value="0" /&gt;
&lt;asp:HiddenField runat="server" ID="hfCustId" /&gt; 
&lt;asp:HiddenField runat="server" ID="hfCarId" /&gt; 
&lt;asp:HiddenField runat="server" ID="hfVanId" /&gt; 
&lt;asp:HiddenField runat="server" ID="hfTruckId" /&gt; 
&lt;asp:HiddenField runat="server" ID="hfMotorCycleId" /&gt; 


var carNode = "&lt;%=VechileNodeTypeEnum.Car.ToString() %&gt;"; 
var selectedCarIds = $('#&lt;%=hfCarId.ClientID %&gt;').val();          
ajaxMethod('/MyPage/MyMethod', { custId: custId, vechileNodeType: carNode, selectedIds: selectedCarIds }, '#carDiv'); 

var vanNode = "&lt;%=VechileNodeTypeEnum.Van.ToString() %&gt;"; 
var selectedVanIds = $('#&lt;%=hfVanId.ClientID %&gt;').val();          
ajaxMethod('/MyPage/MyMethod', { custId: custId, vechileNodeType: vanNode, selectedIds: selectedVanIds }, '#vanDiv'); 

var truckNode = "&lt;%=VechileNodeTypeEnum.Truck.ToString() %&gt;"; 
var selectedTruckIds = $('#&lt;%=hfTruckId.ClientID %&gt;').val();          
ajaxMethod('/MyPage/MyMethod', { custId: custId, vechileNodeType: truckNode, selectedIds: selectedTruckIds }, '#truckDiv'); 

var motorcycleNode = "&lt;%=VechileNodeTypeEnum.MotorCycle.ToString() %&gt;"; 
var selectedMotorcycleIds = $('#&lt;%=hfMotorCycleId.ClientID %&gt;').val();          
ajaxMethod('/MyPage/MyMethod', { custId: custId, vechileNodeType: motorcycleNode, selectedIds: selectedMotorcycleIds }, '#motorcycleDiv'); 


Everything works as expected however I dont like the pattern of repeating the code 4 times with just a few of the proerties changed - I was think I could perhaps use a for loop and loop round 4 times - not sure how I would pass the changed properties dynamically though?
You should create a function that would wrap the ajax call and use it as many times as you wish.

var callMyMethod = function(nodeType, clientId, target) {
  var ids = $('#' + clientId).val();
  ajaxMethod('/MyPage/MyMethod', {
    custId: custId, // is it a global variable?
    vehicleNodeType: nodeType,
    selectedIds: ids 
  }, target);
};

callMyMethod('&lt;%=VechileNodeTypeEnum.Car.ToString()%&gt;', '&lt;%=hfCarId.ClientID%&gt;', '#carDiv');
callMyMethod('&lt;%=VechileNodeTypeEnum.Van.ToString()%&gt;', '&lt;%=hfVanId.ClientID%&gt;', '#vanDiv');
callMyMethod('&lt;%=VechileNodeTypeEnum.Truck.ToString()%&gt;', '&lt;%=hfTruckId.ClientID%&gt;', '#truckDiv');
callMyMethod('&lt;%=VechileNodeTypeEnum.MotorCycle.ToString()%&gt;', '&lt;%=hfMotorCycleId.ClientID%&gt;', '#motorCycleDiv');


If there is some correlation between for example hfCarId.ClientID and #carId, you can remove the target argument from the function.

It should be also possible to generate these calls by iterating over Car, Van, Truck and MotorCycle but I'm not that familiar with .net to suggest it to you.