How to set different color for each letter in a text field using jQuery ? Last Updated : 18 May, 2021 Comments Improve Suggest changes Like Article Like Report In this article, we will find how to set a different color for each letter in a text field using jQuery? Approach: To do this we will have to change the color of text whenever the user enters a letter using the keyboard. For this, we will use onkeydown event that triggers whenever the user presses a key on the keyboard. Syntax: object.onkeydown = function(){Script} Example: Let us look at an example to find how to set a different color for each letter in a text field using jQuery onkeydown event. HTML <!DOCTYPE html> <html> <head> <script src= "https://code.jquery.com/jquery-3.5.0.js"> </script> </head> <style> div { border: 1px solid black; margin-top: 40px; margin-left: 250px; margin-right: 250px; } </style> <body style="text-align: center"> <h2 style="color: green">GeeksforGeeks</h2> <div id="input"></div> <script> function setRange(text) { var r, s; if (document.createRange) { r = document.createRange(); r.selectNodeContents(text); r.collapse(false); s = window.getSelection(); s.removeAllRanges(); s.addRange(r); } } // Selecting input tag and applying css to each word $("#input") .prop("contentEditable", true) .on("input", function () { $(this) .contents() .each(function () { var content = this; $.each($(this).text().split(""), function () { // Generating random color each time var color = "#" + ((Math.random() * 16777215) | 0).toString(16); $("<span>") .text(this) .css({ color: color }) .insertBefore(content); }); $(this).remove(); }); // Function to set the end of the text content setRange($("#input")); }); </script> </body> </html> Output: Change color of text using keydown event Comment More infoAdvertise with us Next Article How to create a Color Input using jQuery Mobile ? F faizamu19 Follow Improve Article Tags : Web Technologies JQuery jQuery-Events jQuery-Questions Similar Reads How to set the text-color for different elements in CSS ? Colors are very important to give a good look to the website and engage the users. So, different colored text elements are added to enhance the visuals of the webpage. The CSS color property defines the text color for an HTML element. For setting text-color for different elements in CSS, add the app 2 min read How to create a Color Input using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Color Input Area using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ h 1 min read How to change the text alignment using jQuery ? In this article, we will see how to change the text alignment of an element using jQuery. To change the text alignment of an element, we will use css() method. The css() method is used to change the style property of the selected element. Syntax: $(selector).css(property) In the below example, firs 1 min read How to set background color for an elements using jQuery ? In this article, we will set the background color for an element using jQuery. To add the background color, we use css() method. The css() method in JQuery is used to change style property of the selected element. The css() in JQuery can be used in different ways. The css() method can be used to che 1 min read How to find an element by text using jQuery ? We will learn how to find an element using jQuery's API. This article requires some knowledge of HTML, CSS, JavaScript, Bootstrap, and jQuery. The elements can be selected based on whether they contain the string we want to find. This can be done using the jQuery contains selector to select elements 2 min read How to find all textareas and makes a border using jQuery ? In this article, we will find all textareas and makes a border using jQuery. To add a border to the textarea element, we use css() method. The css() method is used to change style property of the selected element. The css() in jQuery can be used in different ways. The css() method can used to check 1 min read Like