12

In a similar vain to the c# extension topic on SO It would be great if we could pull together a whole host of useful jQuery function extensions.

Please Note the idea is to have short snippets of code not full blown well known plugins or ui widgets

cletus
  • 616,129
  • 168
  • 910
  • 942
redsquare
  • 78,161
  • 20
  • 151
  • 159
  • The structure of the site is such that, with proper tags and titles, a specific question and answer can be found for a specific search. If i need a logging plugin, i won't search for "[jquery] extension goodies", i'll search for "[jquery-plugin] logging"... or search the jQuery plugin repository. – Shog9 Jan 11 '09 at 17:38
  • funny how this got 3000 views then http://stackoverflow.com/questions/271398/post-your-extension-goodies-for-c-net-codeplex-com-extensionoverflow. – redsquare Jan 11 '09 at 17:42
  • Shog - that turned into a full on project so I think youll have to jump down from that lofty perch you think your on. – redsquare Jan 11 '09 at 17:48
  • Isn't there some other web site this could go on? Doesn't jQuery have such a catalogue? – Rob Jan 11 '09 at 18:32
  • The intention is to have short little snippets of code that people use to save typing...not full blown plugins to provide widgets. – redsquare Jan 11 '09 at 19:57
  • Rob, why what is so wrong about this – redsquare Jan 11 '09 at 20:04
  • @redsquare: i was attempting to point out that you're missing the point of the site. Your example kinda backs this up, as it grew too big and ended up being moved offsite anyway. Sorry if that offends you in some way; i assumed you were familiar with how SO worked... – Shog9 Jan 12 '09 at 00:13
  • I think Jeff et al would be very pleased that such community discussion evolves into offsite projects. You did not offend at all, I just wish some people on SO would be consistent in their opinions (you have contributed and gained big rep points from generic not answerable questions) – redsquare Jan 12 '09 at 00:57
  • Fair enough. If you make it work, then more power to you. – Shog9 Jan 12 '09 at 03:50

9 Answers9

10
// Allows chainable logging
// USAGE: $('#someDiv').hide().log('div hidden').addClass('someClass');
// Demo : http://jsbin.com/odeke
jQuery.log = jQuery.fn.log = function (msg) {
      if ( window.console && window.console.log ) {
         console.log("%s: %o", msg, this);
      }
      return this;
};
redsquare
  • 78,161
  • 20
  • 151
  • 159
4

Interprets absolute URLs on the page as external links, and sets them to open in a new tab and to have friendly title & class for specific styling.

$("#content [href^='http']:not(a:has('img'))").each(function(){$(this).attr("target", "_blank").addClass("external").attr("title", "External Link to " + $(this).attr("href"))});
Antony Carthy
  • 5,549
  • 9
  • 34
  • 38
4

you can use this to see if a selector exists.

if($.exists('#mydiv')) { }

$.exists = function(selector) {
    return ($(selector).length);
}
Jon Erickson
  • 112,242
  • 44
  • 136
  • 174
1

Quick and easy AJAX:

The following allow you to make anchors like

<a href='http://www.google.com/' rel='#myselector' class='ajax' />

which do an AJAX query on the href URL and inject the result into the first element defined by the selector in the anchor's rel attribute.

// Allow hrefs with the class 'ajax' and a rel attribute set with a selector to load content via ajax into the selected element.
$('.ajax').unbind('click').click
(
    function(e)
    {
        $($(this).attr('rel')).load($(this).attr("href"));
        e.preventDefault();
    }
);
Antony Carthy
  • 5,549
  • 9
  • 34
  • 38
1

The validation plug-in is awesome. Used it in an ASP.NET MVC app to dynamically validate stuff on the client using ajax...even returned custom error messages based on the users input...very cool.

Kieron
  • 26,748
  • 16
  • 78
  • 122
0

ah I'm a bit away of the initial question but if there is the "get/set an id" snippet, then I have some code to create unique ids :

$.increment = function (){
    var me = arguments.callee;  
    if (!me.count) me.count = 0;
    return ++me.count;      
}

$.domToSelector = function (jq, options){
    var selectors = [], i = 0; defaults = {}, opts = $.extend(defaults,options);
    $(jq).each(function(){  
        var $node = $(this);    
        if ($node.attr('id')){
            selectors[i] = '#'+$(this).attr('id');      
        }
        else{
             var customId = ''+new Date; 
             customId = customId.replace(/ /g, '').replace(/:/g, '').replace(/\+/g, '');
             customId = customId+'_'+$.increment();
             if (opts.prefix)  customId = opts.prefix+customId;
             $node.attr('id', customId);
             selectors[i] = '#'+customId;        
        }
        i++;    
    });
    if (selectors.length == 1) selectors = selectors[0];    
    return selectors;
}
Olivvv
  • 1,140
  • 1
  • 13
  • 37
0

Extending selectors, i.e. writing your own custom selectors. Here is a sample for two:

$(document).ready(function(){
    $.extend($.expr[':'], {
        inputEmpty: inputEmpty,
        inputNotEmpty: inputNotEmpty
    });
});

function inputEmpty(el) {
    return $(el).val() == "";
}

function inputNotEmpty(el) {
    return $(el).val() != "";
}
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
0

http://plugins.jquery.com/ hosts all manner of plugins, both large and small, and will be a much more comprehensive and useful resource than this thread could ever be, sorry.

nickf
  • 537,072
  • 198
  • 649
  • 721
-2

Just a shortcut to get/set the ID of an element.

 (function($) {
    $.fn.id = function(newDOMID){
        var $this = $(this); 
        if($this.attr('id')){
            if(!newDOMID){
                $this.id.getID($this);
            }
            else {
                $this.id.setID($this,newDOMID);
            }   
        }
        else {
            alert('The target no longer appears to be a part of the DOM!')
        }
    };
    $.fn.id.getID = function($this){
        return $this.attr('id');
    };
    $.fn.id.setID = function($this,newDOMID){
        $this.attr('id',newDOMID);
        return this
    };
})(jQuery);

It's jID on the jQuery plugins site.

redsquare
  • 78,161
  • 20
  • 151
  • 159
User
  • 3,662
  • 6
  • 27
  • 23
  • 1
    Over abstracted IMO. Whats wrong with: `$.fn.id = function(){ return arguments.length>0?this.attr('id',arguments[0]):this.attr('id'); };` – gnarf Aug 21 '09 at 08:56