2011-12-05 12:29:58 +00:00
|
|
|
/**
|
|
|
|
* Version 0.0.1
|
|
|
|
* Fill in an input field from another one (source)
|
|
|
|
* and apply a filter on the string (slugify)
|
|
|
|
* Didier Lafforgue
|
|
|
|
*/
|
|
|
|
$.fn.slugify = function(settings) {
|
2011-12-05 10:31:34 +00:00
|
|
|
|
2011-12-05 12:29:58 +00:00
|
|
|
settings = $.extend({
|
|
|
|
sep: '-'
|
|
|
|
}, settings);
|
2011-12-05 10:31:34 +00:00
|
|
|
|
2011-12-05 12:29:58 +00:00
|
|
|
var target = $(settings.target);
|
|
|
|
target.data('touched', (target.val() != ''));
|
2011-12-05 10:31:34 +00:00
|
|
|
|
2011-12-05 12:29:58 +00:00
|
|
|
var makeSlug = function(event) {
|
|
|
|
var source = $(this);
|
|
|
|
var newVal = source.val().slugify(settings.sep);
|
|
|
|
|
2011-12-05 14:14:50 +00:00
|
|
|
if (!target.data('touched')) {
|
|
|
|
target.val(newVal);
|
|
|
|
target.trigger('change');
|
|
|
|
}
|
2011-12-05 12:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
target.bind('keyup', function(event) {
|
|
|
|
$(this).data('touched', ($(this).val() != ''));
|
|
|
|
});
|
|
|
|
|
|
|
|
return $(this).bind('keyup', makeSlug);
|
|
|
|
};
|