[jQuery] Re: Creating a Function based on text argument
Awesome -- I thought there was something like that, now will it pickup on the jquery functions (like trim) or do I need to program that into it? mkmanning wrote: Operations like .val() return a string, so If you need to extend a string you have to use String.prototype: String.prototype.slug = function(){return this.trim().etc...} The way you've done it works, but in the same way 'trim' works, i.e. you have to pass the string to the function: $.slug($('#form_element').val()); On Oct 21, 3:05 pm, Kerry wrote: I've been looking around and besides my best efforts I haven't been able to figure it out. All the function extending I see extends on an _object_. I want to extend on a string. /** * Returns a slug version of a string */ jQuery.extend ({ slug: function( text ) { return text.trim().toLowerCase().replace( /[^-a-zA-Z0-9\s]/g, '' ).replace( /[\s]/g, '-' ); } }); I want to be able to do something like: $('#form_element').val().slug(); The above does not work. I even tried modifying the 'trim' function in jQuery core to include the rest of the enhancements, but it didn't work.
[jQuery] Re: Creating a Function based on text argument
Operations like .val() return a string, so If you need to extend a string you have to use String.prototype: String.prototype.slug = function(){return this.trim().etc...} The way you've done it works, but in the same way 'trim' works, i.e. you have to pass the string to the function: $.slug($('#form_element').val()); On Oct 21, 3:05 pm, Kerry wrote: > I've been looking around and besides my best efforts I haven't been > able to figure it out. > > All the function extending I see extends on an _object_. > > I want to extend on a string. > > /** > * Returns a slug version of a string > */ > jQuery.extend ({ > slug: function( text ) { > return text.trim().toLowerCase().replace( /[^-a-zA-Z0-9\s]/g, > '' ).replace( /[\s]/g, '-' ); > } > > }); > > I want to be able to do something like: > > $('#form_element').val().slug(); > > The above does not work. I even tried modifying the 'trim' function in > jQuery core to include the rest of the enhancements, but it didn't > work.