Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 4 of 4

Thread: building a simple jQuery plugin

  1. #1

    Clipboard building a simple jQuery plugin

    hello ..
    I basically have a load of code using jQuery, that I now realise I have to add an if statement to every on('mouseenter') event I have depending on whether or not a boolean is true or false.
    Ok - easy. But I wish to build a jQuery plugin (at the most simplest level), so that my query will continue and chain - or break. Is there a jQuery method that already exists that does that?

    Otherwise::
    Code:
    var myBreak = true;
    jQuery.fn.makeOrBreak = function(v){    
    // break chain or query
    if(v != true){
    }else{
    // keep calm and carry on
    }
    };
    // usage example
    $('#id').makeOrBreak(myBreak).hide(); // boolean is true so '#id' is allowed to chain and 'hide()'
    maybe there's easier ways - but it's interesting to build a plugin. And I have around 40 mouse overs ive got to plug an if statement into ...
    Any pointers!?

    PS am i in the right forum?!

  2. #2
    I would write that like this:
    Code:
    myBreak && $("#id").hide();
    This evaluates as "If myBreak == true, hide the element"
    AS2 / AS3 / JS / JQUERY / (X)HTML / HTML5 / CSS / CSS3 / PHP

  3. #3
    handy tip! Shorthand is so confusing when you start learning code - but brilliant when sailing.

    Still - is it possible to break out of the chain in jQuery?

  4. #4
    Still - is it possible to break out of the chain in jQuery?
    Just return something else other than the jquery collection object in the function:

    Code:
    jQuery.fn.test = function() {
       return false; // Breaks the chain
    };
    This will break the chain, but will obviously generate errors if there are function calls after it. If you know what methods are going to be called you can create a mock object that has the same methods. Your mock object to return if you knew the hide() method was going to be called would be something like this:

    Code:
    var mock = {
        hide: function() {
             return this;
        }
    };
    jQuery.fn.test = function() {
       if (true) {
           return mock;
       } else {
           return this;
       }
    };

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012