mercredi 25 février 2015

I can't update a properties value after a change event

I have a change event and an object that I would like to update. the value of a property in the object should change when the event occurs. the change event acts on the click of a checkbox. when the checkbox is checked a random number is produced with a digit before a decimal place and when it is unchecked a random number without the ones place is produced. I want to put that number in the items object for the value of price and I don't know how to do that. that number should always be updated whenever the checkbox is clicked.


So the user will be playing an adding game that has an option to remove the ones place to make it easier.


here is some code.



//make a div all the same size with different colors to put for pic value
$(document).ready( function(){
var banana = $("<div class = 'banana box'>banana</div>");
var apple = $("<div class = 'apple box'>apple</div>");
var apple = $("<div class = 'pears box'>apple</div>");
var apple = $("<div class = 'oranges box'>apple</div>");
var apple = $("<div class = 'steak box'>apple</div>");

var WithOrWithout = (function(){
return {
PriceWithOnes : function(min, max){
return Math.random() * (max-min) + min
},
PriceWithOutOnes : function(min, max){
return Math.random() * (max-min) + min
}
}
})();

// i want to return price so the property price in the object items gets the price
$("#withones").change(function(){
if(this.checked){
price =WithOrWithout.PriceWithOnes(0, 5);
$(this).after(price)
console.log($(this))
console.log(price)

//How do I return price to to items.price below.
}else{
price =WithOrWithout.PriceWithOutOnes(0, 1);
$(this).after().html("un-checked")
$(this).after(price)

}
})
items = {
banana : {name : "banana","price" : "$1.99", "pic" : banana}, // the price will be gotten from the change event
apple : {name : "apple","price" : "$1.49", "pic" : apple}
}

$('body').append(items.banana.pic)
$('body').append(items.apple.pic)
console.log(items.apple )


function AmountToDisplay(){
$emitter = $({});
$('#amountItems').change(function(){
var amountchosen = $("#amountItems option:selected").val();
$emitter.trigger('amountUpdate', amountchosen);
});



$emitter.on('amountUpdate', function(e, val){
val = arguments[1];
if(val == 2){
myEventHandlerfor2(val,e);
}else if(val == 3){
myEventHandlerfor3(val,e);
}else if(val == 4){
myEventHandlerfor4(val,e);
}
})
}
console.log(AmountToDisplay()) //return undefined

function myEventHandlerfor2(){
alert("2 clicked")
}




Aucun commentaire:

Enregistrer un commentaire