Solomon Closson
Solomon Closson

Reputation: 6217

jQuery extend method with parameter?

Using the following jQuery:

jQuery.fn.extend({
    resizelis: function(xWidth) {
      var mButtons = this.length;
      var buttonSize = (xWidth / mButtons);
        return this.each(function() {
            this.css("width", buttonSize + "px");
            this.children("a").css("width", buttonSize + "px");
        });
    }
});

And I use the method like so:

var $width = $window.scrolltop() > $("#header_image").outerHeight() ? .92 * $(window).width() : .92 * $(".content").width();
$(".menu").children("li").resizelis($width);

HTML

<ul class="menu">
    <li class="current"><a accesskey="1" href="index.html">Home</a></li>
    <li><a accesskey="2" href="highlights.html">Highlights</a></li>
    <li><a accesskey="3" href="agenda.html">Agenda</a></li>
    <li><a accesskey="4" href="tracks.html">Tracks</a></li>
    <li><a accesskey="5" href="sponsors.html">Sponsors / Exhibits</a></li>
    <li><a accesskey="6" href="travel.html">Travel</a></li>
    <li><a accesskey="7" href="register.html">Register</a></li>
</ul>

But this gives me an error. What am I doing wrong exactly? Thanks.

Upvotes: 2

Views: 1001

Answers (1)

Hanlet Esca&#241;o
Hanlet Esca&#241;o

Reputation: 17380

Upon debugging your code, I found that within the extend method you use this instead of $(this), and you try to use the css method with this, which is not a jQuery object. Try changing this to $(this), and try again:

jQuery.fn.extend({
    resizelis: function(xWidth) {
      var mButtons = this.length; //same as with the jquery object $(this)
      var buttonSize = (xWidth / mButtons);
        return $(this).each(function() 
       {
            $(this).css("width", buttonSize + "px");
            $(this).children("a").css("width", buttonSize + "px");
        });
    }
});

Inside the each loop, this is a HTMLLIElement, to use the css method, use $(this).

Upvotes: 3

Related Questions