Never been to CodeSnippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)

jQuery.attrToClass (See related posts)

Jquery function that appends a css class to all designated elements on a page by extracting a specific attribute from that element.

For instance, the example code below would change

<input type="submit" name="submit" value="Submit" />

to:

<input type="submit" name="submit" value="Submit" class="submit" />


jQuery.fn.attrToClass = function(attribute) {
	$(this).each(
		function(intIndex){
		    $(this).addClass($(this).attr(attribute));
	});
};

$(document).ready(function(){
        // designate the tag and the attribute to be extracted
	$("input").attrToClass("type");
});

You need to create an account or log in to post comments to this site.