0

Why the value of oldHeight is not overridden ? It still has oldHeight to 80 not what i have passed as argument. Even if i pass argument newHeight, it will also not override it. But according to the documentation, it will automatically updated. Here's the code:

( function($) {
$.fn.bad = function(callback) {
    var options;
    var settings = $.extend({
        "oldHeight":"80",
        "newHeight":"200"
    }, options);
    return this.each( function() {
        var that$ = $(this);
        that$.bind("mouseenter", function() {
            $(this).animate({ "height" : settings.newHeight+"px" });
        }).bind("mouseleave", function() {
            $(this).animate({ "height" : settings.oldHeight+"px" });
        });
    });
}
})(jQuery);
$(".box").bad({"oldHeight":"400"});

And this ===> Fiddle

3
  • It is overriding, isn't it? the box is getting bigger on the fiddle Commented Feb 27, 2013 at 10:43
  • hey, i have assigned 400 when the mouseleave but it is limited to default option. Commented Feb 27, 2013 at 10:44
  • oops, my bad .. misread the question! Commented Feb 27, 2013 at 10:44

2 Answers 2

1

You initialized "options" with null with var options; and extended your settings with an empty value.

( function($) {
    $.fn.bad = function(options) {
            var settings = $.extend({
                "oldHeight":"80",
                "newHeight":"200"
            }, options);

    ...

this works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i just started. I didn't noticed that i have already callback as parameter. Just was following the documentation and got confused. I didn't even noticed that i have to assign that callback variable to $.extend({}) method.
1

Try ths,

var settings = {};
var settings = $.extend(settings, {
    "oldHeight":"80",
    "newHeight":"200"
}, options);

Your function needs to accept the options not the callback (if there is no callback being used).

So your code will be,

(function($) {
$.fn.bad = function(options) {
    var settings = {};
    var settings = $.extend(settings, {
        "oldHeight":"80",
        "newHeight":"200"
    }, options);
    ...

Test

3 Comments

I just named it callback. However, naming options is not mandatory. jsfiddle.net/cGSNn/5
Yes, it's not mandatory, but makes more sense semantically :)
well, ok. +1 for the effort.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.