Search code examples
javascriptjquery-ui-slider

How do I communicate between two javascript object literals?


I want to know how I can call a method from an object within another object literal. Here is my code. I am building a jQuery UI slider. I am trying to call the animatebox function within the animations object literal and the error in my chrome console is:

Uncaught TypeError: Cannot call method 'animateBox' of undefined

My code looks like this:

var sliderOpts = {

    animate: true,
    range: "min",
    value: 50,
    min: 10,
    max: 100,
    step: 10,

    //this gets a live reading of the value and prints it on the page
    slide: function (event, ui) {
        $("#slider-result").html(ui.value + " GB");
    },

    //this updates the hidden form field so we can submit the data using a form
    change: function (event, ui) {
        $('#hidden').attr('value', ui.value);
        var val = ui.value,
            $box = $('#message');
        switch (true) {
        case (val > 50):
            $box.animations.animateBox().html('you have chosen xtremenet');
            break;
        case (val < 50):
            $box.fadeOut().fadeIn().html('you have chosen valuenet');
            break;
        default:
            $box.fadeOut().fadeIn().html('you have chosen powernet');
        }
    }
};

var animations = {
    animateBox: function () {
        $("#message").slideDown(500);
    }
};

$(".slider").bind("slidecreate", animations.animateBox).slider(sliderOpts);

So how do I call this method called animateBox?


Solution

  • var animations = {
        animateBox: function (obj) {
            obj.slideDown(500);
        }
    };
    
    
    animations.animateBox($box.html('you have chosen xtremenet'))