In this solution, I first dynamically add a style block to <head> when the page loads:
// add style block to the <head> with default placeholder css on page load var defaultColor = 'BBBBBB'; var styleContent = 'input:-moz-placeholder {color: #' + defaultColor + ';} input::-webkit-input-placeholder {color: #' + defaultColor + ';}'; var styleBlock = '<style id="placeholder-style">' + styleContent + '</style>'; $('head').append(styleBlock);
Then, to change the color of the placeholder text, I just update the contents of the style block:
var randomColor = Math.floor(Math.random()*16777215).toString(16); styleContent = 'input:-moz-placeholder {color: #' + randomColor + ';} input::-webkit-input-placeholder {color: #' + randomColor + ';}' $('#placeholder-style').text(styleContent);
Check jsFiddle:
Change text color with jQuery
source share