Getting values from array of same class elements in jquery
-
<tr> <td class="moduleSupport"> 1 </td> <td class="moduleName"> Cargo Hold </td> <td class="currentModuleQuantity"> <input class="currentModuleQuantity" placeholder="Enter current quantity" type="text"> <span class="currentModuleQuantityErr"></span> </td> </tr> <tr> <td class="moduleSupport"> 1 </td> <td class="moduleName"> Crow Nest </td> <td class="currentModuleQuantity"> <input class="currentModuleQuantity" placeholder="Enter current quantity" type="text"> <span class="currentModuleQuantityErr"></span> </td> </tr> <tr> <td class="moduleSupport"> 1 </td> <td class="moduleName"> Galley </td> <td class="currentModuleQuantity"> <input class="currentModuleQuantity" placeholder="Enter current quantity" type="text"> <span class="currentModuleQuantityErr"></span> </td> </tr> <tr> <td class="moduleSupport"> 1 </td> <td class="moduleName"> Information Console </td> <td class="currentModuleQuantity"> <input class="currentModuleQuantity" placeholder="Enter current quantity" type="text"> <span class="currentModuleQuantityErr"></span> </td> </tr>
Then I have similar code later on where I need to make use of these values to work out some prices for me. Here is my jquery code that get the current values for me:
-
var calculateShipUpgradeCost = { init: function(config){ this.config = config; this.bindEvents(); },
-
bindEvents: function() { // let's create an alias of the object in order to // use it inside jQuery callback without loosing the scope var self = this; this.config.currentModuleQuantity.on('click', function (e) { // lets store the trigger and pass it to the functions var $trigger = $(this); self.checkCurrentModuleQuantity($trigger); // self.checkCurrentQuaterQuantity(); }); },
-
checkCurrentModuleQuantity: function($trigger) { console.log('Click event detected in ship modules section'); var module_support = $trigger.parents('tr').find('.moduleSupport').text(); // output the text content of the current moduleSupport td by accessing the parent tr first and then finding the td.moduleSupport console.log('Current module support value:', module_support); }
-
//Initiate the object
calculateShipUpgradeCost.init({
currentModuleQuantity: $('input.modulesWanted'),
currentModuleQuantityErr: $('span.currentModuleQuantityErr'),
currentQuaterQuantity: $('input.currentQuaterQuantity'),
currentQuaterQuantityErr: $('span.currentQuaterQuantityErr'),
currentModules: $('table'),
moduleSupport: $('td.moduleSupport'),
upgradeModQantity: $('td.upgradeModQantity')
});
How can I make use of these values I am getting in another function in the bindEvents with an on('keyup',function(){}?