Make a function re-usable ?
So i have 2 functions that actualy do exactly the same but on an other event. It are pretty "large" scripts and so i would like to make 1 out of it, but i don't know how to do that and i think it will come in handy a lot if i knew how. So who could show me please ?
- var calculate = function(){
$('#loading').hide(); // ladenprentje verstoppe bij t begin
$(".code").live("keypress", function(e){
if (e.keyCode == 9 ) {
// rij waarin we zitte zoeken en opslaan
var row = $(this).closest(".item-row"),
// code die we ingeven
code = row.find(".code").val();
if (code != ""){
$.getJSON("../js/producten.js", function(json) {
// door het fileke loopen producten.js
$.each(json.channel.items,function(i,item) {
// als het item in de array items[i] overeenkomt met de naam dat ik opgeef
if(json.channel.items[i].code == code) {
GezochtePrijs = json.channel.items[i].prijs;
GezochteOmschrijving = json.channel.items[i].omschrijving;
var aantalBesteld = parseInt(row.find(".qty").val());
var ItemsInStock = parseInt(json.channel.items[i].instock);
var ItemsLeft = ItemsInStock - aantalBesteld;
if(ItemsInStock < aantalBesteld){
//alert("er zijn nog maar "+ItemsInStock+" items in stock.");
row.find(".qty").css("background-color","red");
row.find(".qty").css("color","white");
}else{
if(ItemsLeft < 3){
row.find(".qty").css("background-color","orange");
row.find(".qty").css("color","white");
}else{
if( ItemsInStock > aantalBesteld){
row.find(".qty").css("background-color","white");
row.find(".qty").css("color","black");
}
}
}
ItemsLeft = parseInt(ItemsInStock)-1;
row.find(".cost").html("€"+GezochtePrijs);
row.find(".description").html(GezochteOmschrijving);
// alert("DEBUG: Item werd gevonden");
var price = parseFloat(GezochtePrijs) * parseFloat(row.find('.qty').eq(0).val());
row.find('.price').html(isNaN(price) ? 'N/A' : price.toString());
update_total() ;
}
}); // end each json
});// end json
//end if code
}else{
alert("Gelieve artikelcode op te geven");
}
//$('.code').closest(".price").eq[0].focus();
} // end if keycode
}); // end function
};
var recheck = function(){
$('#loading').hide(); // ladenprentje verstoppe bij t begin
--> ONLY LINE THAT CHANGES $(".qty").live("keypress", function(e){
if (e.keyCode == 9 ) {
// rij waarin we zitte zoeken en opslaan
var row = $(this).closest(".item-row"),
// code die we ingeven
code = row.find(".code").val();
if (code != ""){
$.getJSON("../js/producten.js", function(json) {
// door het fileke loopen producten.js
$.each(json.channel.items,function(i,item) {
// als het item in de array items[i] overeenkomt met de naam dat ik opgeef
if(json.channel.items[i].code == code) {
GezochtePrijs = json.channel.items[i].prijs;
GezochteOmschrijving = json.channel.items[i].omschrijving;
var aantalBesteld = parseInt(row.find(".qty").val());
var ItemsInStock = parseInt(json.channel.items[i].instock);
var ItemsLeft = ItemsInStock - aantalBesteld;
if(ItemsInStock < aantalBesteld){
//alert("er zijn nog maar "+ItemsInStock+" items in stock.");
row.find(".qty").css("background-color","red");
row.find(".qty").css("color","white");
}else{
if(ItemsLeft < 3){
row.find(".qty").css("background-color","orange");
row.find(".qty").css("color","white");
}else{
if( ItemsInStock > aantalBesteld){
row.find(".qty").css("background-color","white");
row.find(".qty").css("color","black");
}
}
}
ItemsLeft = parseInt(ItemsInStock)-1;
row.find(".cost").html("€"+GezochtePrijs);
row.find(".description").html(GezochteOmschrijving);
// alert("DEBUG: Item werd gevonden");
var price = parseFloat(GezochtePrijs) * parseFloat(row.find('.qty').eq(0).val());
row.find('.price').html(isNaN(price) ? 'N/A' : price.toString());
update_total() ;
}
}); // end each json
});// end json
//end if code
}else{
alert("Gelieve artikelcode op te geven");
}
//$('.code').closest(".price").eq[0].focus();
} // end if keycode
}); // end function
};