Best practice implementation

Best practice implementation

Im looking to develop a plugin that can apply common functionality to multiple elements.

My issues is that if the state of the current "clicked" element is set to display then any other displaying elements must be hidden. What would be the best way to actually do this? I have done something before using prototyping where I stored the current displaying element as a variable in the object and hiding it on new element click and then storing the new element.

NOTE: the "hide" class comes from Twitter bootstrap which is just setting "display: none".

Example HTML:

  1. <div class="speech-bubble">
  2. <img src="icon"/>
  3. <div class="bubble hide">I'm some text</div>
  4. </div>
  5. <div class="speech-bubble">
  6. <img src="icon"/>
  7. <div class="bubble hide">I'm more text</div>
  8. </div>
Simplified jQuery plugin concept:
  1. $.fn.speechBubble = function(){
  2. return this.on('click', function(){
  3. /* hide any possible open bubbles here? Maybe call a method that closes ALL? */
  4.                 //change bubble visual state
  5. $(this).find('.bubble').toggle();
  6. });
  7. }
Example implementation:
  1. $('.speech-bubble').speechBubble();
------------------------------------------------------

Also is it possible to access a "close" method of all the bubble created element's so as to close all without actually running the "initialise" state eg:
  1. $('.speech-bubble').speechBubble.close();
Would I be looking at doing something similar to the explanation at:  http://learn.jquery.com/plugins/advanced-plugin-concepts/ ?