I have used the answers here as an examples although I would prefer to write it like this: value.stringToSlug()
So I have changed it to this:
// String to slug
String.prototype.stringToSlug = function(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
str = str.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
};
It works if I pass the string like this:
var value = $(this).val();
value.stringToSlug(value);