jQuery slugify plugin

1
2
3
4
5
6
7
8
jQuery.fn.slugify = function(obj) {
    jQuery(this).data('obj', jQuery(obj));
    jQuery(this).keyup(function() {
        var obj = jQuery(this).data('obj');
        var slug = jQuery(this).val().replace(/\s+/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
        obj.val(slug);
    });
}

More like this

  1. Client-side django template with jQuery by ksjun 4 years, 11 months ago
  2. Orderable inlines using drag and drop with jQuery UI by simon 4 years, 9 months ago
  3. Make hyperlinks for labels of raw_id_fields (jQuery) by ramen 3 years, 5 months ago
  4. Admin Input Field Character Count via jQuery by joshman 4 years, 6 months ago
  5. Disable fields in oldforms admin using jQuery by schraal 5 years ago

Comments

gobble (on July 5, 2010):
jQuery.fn.slugify = function(obj) {
    jQuery(this).data('obj', jQuery(obj));
    jQuery(this).keyup(function() {
        var obj = jQuery(this).data('obj');
        var slug = jQuery(this).val().replace(/\s+/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
        jQuery(this).data('obj').val(slug);
    });
    jQuery(this).blur(function() {
        var obj = jQuery(this).data('obj');
        var slug = jQuery(this).val().replace(/\s+/g,'-').replace(/[^a-zA-Z0-9\-]/g,'').toLowerCase();
        jQuery(this).data('obj').val(slug);
    });

}

this fixes a blank slug field if you select a entry for your lineedit from your prowsers suggestion dropdown and exit the lineedit with |tab|.

#

pmcelhaney (on May 23, 2011):

Here's a more advanced version. It takes care of a few things like converting & to "and", preventing leading or trailing spaces from becoming hyphens, and barring automatic changes after the slug has been edited manually. It also accepts an optional slugFunc parameter so you can customize how the title is converted to a slug.

https://github.com/pmcelhaney/jQuery-Slugify-Plugin

#

(Forgotten your password?)