Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
I made text with different colors by listening to keypress events and generating a span with random colors using style.I feel there is alot of overhead how can this be done better?.please help me

the link to codepen is below:

Codepen

What I have tried:

I made text with different colors by listening to keypress events and generating a span with random colors using style.I feel there is alot of overhead how can this be done better?.please help me

the link to codepen is below:

Codepen
Posted
Updated 15-Jun-16 19:23pm
Comments
Beginner Luck 15-Jun-16 4:57am    
You should depend more on CSS than in javascript.

1 solution

try
JavaScript
$(document).on("keypress", function (e) {
  
  if(e.keyCode == 32)
    {
      $(".editable").append("<span>"+" "+"</span>");
	}
	else if(e.keyCode == 8)
    {  
      $(".editable span:last-child").remove();
	}
  
	else {
			var s = String.fromCharCode(e.which);
			
			var color = '#' + (Math.random() * 0xFFFFFF << 0).toString(16);
			$(".editable").after($("img")).append("<span class='text' style='color:"+color+";'>"+s+"</span>");
  
	}
});


and add class for shadow effect to text.

CSS
.text
{
text-shadow: 0px 1px 1px rgba(0, 0, 0, 0.7);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900