Created
January 6, 2015 09:27
-
-
Save malachi358/fac9a74411506d1a2793 to your computer and use it in GitHub Desktop.
Change Color by character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//HTML ==> | |
<table> | |
<tr> | |
<td class="colorizeMe">foooo_bar</td> | |
</tr> | |
<tr> | |
<td class="colorizeMe">hello_world</td> | |
</tr> | |
<tr> | |
<td class="colorizeMe">dubidubi_doo</td> | |
</tr> | |
</table> | |
----------------------------------------------------------- | |
//CSS ==> | |
.red { color:red; } | |
.blue { color:blue; } | |
----------------------------------------------------------- | |
//Javascript==> | |
var colorize = function(text) { | |
var delimiter = "_", | |
parts = text.split(delimiter), | |
firstPart = $("<span>", { "class": 'red', html: parts[0] }), | |
secondPart = $("<span>", { "class": 'blue', html: text.split(parts[0] + delimiter)[1] }); | |
return $("<div>").append(firstPart).append(delimiter).append(secondPart).html(); | |
}; | |
var colorizeElements = function($elements) { | |
// $elements has to be something like $("td.classname") | |
$elements.each(function() { | |
$(this).html(colorize($(this).html())); | |
}); | |
}; | |
// this is your actual call | |
colorizeElements($("td.colorizeMe")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment