Skip to content

Instantly share code, notes, and snippets.

@esundahl
Created March 30, 2012 22:17
Show Gist options
  • Save esundahl/2256077 to your computer and use it in GitHub Desktop.
Save esundahl/2256077 to your computer and use it in GitHub Desktop.
Javascript: jQuery: Style first word plugin
(function($) {
$.fn.styleFirstWord = function(attr) {
if ( typeof attr !== 'object' ) {
attr = {
class: attr || 'firstWord'
};
}
return this.each(function() {
var $this = $(this),
headingText = $this.text().split(' '),
span = $('<span>', attr).text(headingText.shift());
$this.html(
$('<div>').append(span).html() + ' ' + headingText.join(' ')
);
});
};
})(jQuery);
// Usage
// $('h1').styleFirstWord('flash')
// $('h1').styleFirstWord({
// id: 'first-word',
// class: 'some-class'
// })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment