Solved

Jquery plugin authoring

Posted on 2010-08-24
6
360 Views
Last Modified: 2013-11-11
Hi,
I need to reuse the same bit of code numerous times, so i'm figuring plugins for jquery are probably the way to go. The examples though seem to be quite complex and I cant get my head around them.
Can anyone point me in the right direction please?
Here is the code I am going to use for the plugin, I just need to know how to modify it as a plugin:
<script type="text/javascript">
          $('.tabs').find('div:first').siblings('div').hide();
          $('.tabs > ul > li >a').click(function(e){
              container=$(this).parent().parent().parent();
              $(this).parent().addClass('selected').siblings().removeClass('selected');
              $('.'+$(this).attr('rel'),container).show().siblings('div').hide();//Show the div with the content in
              e.preventDefault();
          });
      </script>

Open in new window


Which will be used in conjunction with the following markup:

 <div class="tabs">
          <ul>
              <li><a href="#" rel="tab1">Tab1</a></li>
              <li><a href="#" rel="tab2">Tab2</a></li>
              <li><a href="#" rel="tab3">Tab3</a></li>
          </ul>
          <div class="tab1">Tab 1 content</div>
          <div class="tab2">Tab 2 content</div>
          <div class="tab3">Tab 3 content</div> 
      </div>

Open in new window

0
Comment
Question by:jdav357
  • 3
  • 2
6 Comments
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33511830
Could you please rephrase the question a little, as i couldn't understand the requirement?
Thanks
0
 
LVL 2

Author Comment

by:jdav357
ID: 33511886
With my javascript I have attached, I need to change it to a plugin, so that I can just add a class or id of tabbed for example to an element and it will use my function above!
0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33511902
please mention what your code is trying to do.
0
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
LVL 15

Expert Comment

by:SRigney
ID: 33511925
This tutorial seems to be an easier one to understand.

http://www.queness.com/post/112/a-really-simple-jquery-plugin-tutorial
0
 
LVL 2

Author Comment

by:jdav357
ID: 33511927
[code]
<script type="text/javascript">
          $('.tabs').find('div:first').siblings('div').hide();
          $('.tabs > ul > li >a').click(function(e){
              container=$(this).parent().parent().parent();
              $(this).parent().addClass('selected').siblings().removeClass('selected');
              $('.'+$(this).attr('rel'),container).show().siblings('div').hide();//Show the div with the content in
              e.preventDefault();
          });
      </script>
[/code]
This is my code that I am trying to convert in to a plugin. It is for a special kind of tabs that I require. Thanks for your help.
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 500 total points
ID: 33512037
i think you code is already in shape, just add this
<<# (function($){>>  in the beginning
and <<})(jQuery);  >> in the end

http://net.tutsplus.com/articles/news/learn-how-to-create-a-jquery-plugin/
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question