I want to write my first jQuery plugin

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

I've edited plugins to suit my needs and I think it's time I write my own. I know what I want it to do, I'm just not sure where to start.

I want to create a plugin called "lightSwitch". I'd name it "switch" but I don't want to interfere with javascripts' namespace. Basically you call an element, attach an event handler, then use the lightSwitch effect to change something. The plugin being used would look like this:

JAVASCRIPT Code: [ Select ]
 
// begin a function on an event such as click
$('#element').click( function() {
   
    // now call the lightSwitch plugin
    $(this).lightSwitch( function() {
 
          // this is what the change will be
          $(this).css("borderColor", "red");
   
   });
 
});
 
  1.  
  2. // begin a function on an event such as click
  3. $('#element').click( function() {
  4.    
  5.     // now call the lightSwitch plugin
  6.     $(this).lightSwitch( function() {
  7.  
  8.           // this is what the change will be
  9.           $(this).css("borderColor", "red");
  10.    
  11.    });
  12.  
  13. });
  14.  


What I want the plugin to do is store the original state of the element it's being called on, then initiate the function within the lightSwitch. After that's completed, if the event happens again, the element with the lightSwitch call will return to it's original values.

So say in the above example the original "#element" had a blue border. When the user clicks on "#element" the border changes to red. Then if the user clicks on it again, it changes back to blue.

Is there already a jQuery effect that does this?

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: No registered users and 38 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.