0

This is my actual code:

$.fn.menuWidth = (function(){
  return {
    show: function(a){
      this.animate({
        width: 400
      });
    },
    hide: function(a){
      return "hide";
    }
  };
}());

$("#header").menuWidth.show();

But, i can't access the $("#header") from the show object. Any idea how to make it work?

2
  • 1
    did you try to do $("#header").menuWidth().show(); Commented Jul 13, 2015 at 19:12
  • plugins should not return methods , rather should return this which allows them to be chainable. Not hard to find lots of tutorials on jQuery plugins Commented Jul 13, 2015 at 19:14

2 Answers 2

3

You might want to change the structure of your plugin definition to something like this

$.fn.menuWidth = function(options) {
  this.show = function() {
    this.animate({
      width: 400
    });
    return this; // <---- This will enable chaining
  };
  this.hide = function() {
    return "hide"; 
  };
  return this; // <--- returning this will make visible the hide and show methods.
};

console.log($("#header").menuWidth().show().hide());

Doing jQuery's show and hide is also possible but this might sound a bit like a hack.

$($("#header").menuWidth().show()).hide();

menuWidth plugin returns this which is the instance of the element. Wrapping it into a jQuery object will let us perform .hide() and .show().

This will not be possible if .hide() of menuWidth is invoked because it does not return the element instance.

PS: This is a very very simple way to write a plugin. There are better and much more complex ways to write plugins.

$.fn.menuWidth = function(options) {
  this.show = function() {
    this.animate({
      width: 400
    });
    return this;
  };
  this.hide = function() {
    return "hide";
  };
  return this;
};

console.log($("#header").menuWidth().show().hide());
#header{
width: 100px;
  border: 1px solid red;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">
  This is a header
  </div>

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

5 Comments

There is possibility to occur any conflict with native jQuery .show() function?
@CaioKawasaki yes, approach shown would over write jQuery default show for that element
@CaioKawasaki: Yes, it would override the show and hide methods. However, it is still possible to achieve that. .show method of menuWidth returns the jquery instance of the element. Wrapping that with a jquery object will let you perform jQuery's show and hide. Please see my updated answer
Like @charlietfl said, the override will happen just for $("#header"). Right?
@CaioKawasaki: Yes, just along that chain. If you break the chain and wrap into a jquery object, you will be able to perform all jquery operations. I hope I made some sense :-)
1

You will want to change your function signature to menuWidth = function(action) {}; then pass 'show' or 'hide' to the menuWidth() call. See https://learn.jquery.com/plugins/basic-plugin-creation/#minimizing-plugin-footprint for jQuery's example documentation for this.

$.fn.menuWidth = function(action) {
  if (action == 'show') {
      this.animate({
        width: 400
      });
  }
  else if (action == 'hide') {
     alert('hide');
  }

  return this;
};

$("#header").menuWidth('show');
$("#header").menuWidth('hide');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="header">Testing</div>

3 Comments

This is a good answer too, however I prefer the chaining method, I'm sorry, I will accept the another answer...
@CaioKawasaki No problem and I agree that the other answer is just as good as I upvoted it also.
this is actually a more common pattern used IMO ..passing params instead of chained methods within the plugin but normally would return this so other jQuery methods are still available

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.