Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery object never changing except for first time

I ran into a problem with an object which I'm trying to modify. The object has a certain amount of keys in the format key_yyyy-mm-dd. When certain inputfields lose focus, I trigger a function to modify the object. This function is as follows:

function updateHotelBooking()
    {
        $(".no_hotel").each(function(i) {
            var day = $(this).attr('name').match(/\[(.*?)\]/)[1];
            hotelBooking["key_" + day] = parseInt($(this).val());
        }); 
    }

.no_hotel are the textboxes that trigger the function, and they also provide a value which I want to put in my object.

Now, say I put 3 in my first text box, a console.log will return the following object:

Object
key_2011-08-21: 3
key_2011-08-22: 0
key_2011-08-23: 0
key_2011-08-24: 0
key_2011-08-25: 0

However, the next time I put something in the textbox (or another textbox that should trigger the function), it DOES trigger, however the object returned remains the same. So instead of changing the first number to, say, 5, it will just return 3 again.

I have no idea why. My code seems pretty straightforward, and a console.log of day and $(this).val() returns the right values. It's just my object that doesnt get updated.

Does anyone have any idea? Thanks a lot!

EDIT:

hotelBooking is initialized right after $(document).ready():

var hotelBooking = {};

The method that calls updateHotelBooking is the following:

$(".roomrequest").blur(function()
{
    updateHotelBooking();
});

EDIT2: JSFiddle: http://jsfiddle.net/pBYeD/2/

like image 674
Joris Ooms Avatar asked Nov 04 '22 15:11

Joris Ooms


1 Answers

it has to do with something with the console rather than your code, if you change the logging code to this, you will see that you have the correct values:

    function updateHotelBooking()
    {
        $(".no_hotel").each(function(i) {
            var day = $(this).attr('name').match(/\[(.*?)\]/)[1];
            hotelBooking["key_" + day] = parseInt($(this).val());     
            **logObject(hotelBooking);**      
        });     
    }  


function logObject(hotelBooking){
        for(var i in hotelBooking){
            console.log(i+": "+hotelBooking[i]);
        }
        console.log("------");
    }
like image 150
kabaros Avatar answered Nov 09 '22 11:11

kabaros