javascript - Random generated colors and saving them in CSS -
i'm won't able explain 100% because if figure out.
i'm trying code chat room. want use javascript randomly generate color , assign someone's name. assign once , when type new message defaults. how make color sticks? have far:
$(document).ready(function() { $('.username').each(function () { var hue = 'rgb(' + (math.floor((256-199)*math.random()) + 200) + ',' + (math.floor((256-199)*math.random()) + 200) + ',' + (math.floor((256-199)*math.random()) + 200) + ')'; $(".username").css("color", hue); }); });
try using $(this) instead of $(".username") inside each function:
$('.username').each(function () { var hue = 'rgb(' + (math.floor((256-199)*math.random()) + 200) + ',' + (math.floor((256-199)*math.random()) + 200) + ',' + (math.floor((256-199)*math.random()) + 200) + ')'; $(this).css("color", hue); });
Comments
Post a Comment