Random Colorful letters
I have this code:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$('#list').html(result);
//Here should call a function to color all the words of the div
}
});
</script>
});
I'm trying to color all letters every 3 seconds using the setTime ()
function.
Note: I'm trying to color each letter of a word, in other words, each letter will have a color
Like:
https://i.imgur.com/Tw2Q58U.png
I tried with this code, but it changes the color of the entire div(The div stay with only one color):
var randomColor = Math.floor(Math.random()*16777215).toString(16);
document.getElementById('list').style.color = '#' + randomColor
javascript html
add a comment |
I have this code:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$('#list').html(result);
//Here should call a function to color all the words of the div
}
});
</script>
});
I'm trying to color all letters every 3 seconds using the setTime ()
function.
Note: I'm trying to color each letter of a word, in other words, each letter will have a color
Like:
https://i.imgur.com/Tw2Q58U.png
I tried with this code, but it changes the color of the entire div(The div stay with only one color):
var randomColor = Math.floor(Math.random()*16777215).toString(16);
document.getElementById('list').style.color = '#' + randomColor
javascript html
It seems you'll need<span>
elements for each letter you'd like to color.
– ggorlen
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
CC_List1
are incorrect, the correct islist
. I will edit the post, sorry.
– Bro Faz Sol
4 hours ago
2
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago
add a comment |
I have this code:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$('#list').html(result);
//Here should call a function to color all the words of the div
}
});
</script>
});
I'm trying to color all letters every 3 seconds using the setTime ()
function.
Note: I'm trying to color each letter of a word, in other words, each letter will have a color
Like:
https://i.imgur.com/Tw2Q58U.png
I tried with this code, but it changes the color of the entire div(The div stay with only one color):
var randomColor = Math.floor(Math.random()*16777215).toString(16);
document.getElementById('list').style.color = '#' + randomColor
javascript html
I have this code:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$('#list').html(result);
//Here should call a function to color all the words of the div
}
});
</script>
});
I'm trying to color all letters every 3 seconds using the setTime ()
function.
Note: I'm trying to color each letter of a word, in other words, each letter will have a color
Like:
https://i.imgur.com/Tw2Q58U.png
I tried with this code, but it changes the color of the entire div(The div stay with only one color):
var randomColor = Math.floor(Math.random()*16777215).toString(16);
document.getElementById('list').style.color = '#' + randomColor
javascript html
javascript html
edited 3 hours ago
ScrapCode
1,00331327
1,00331327
asked 4 hours ago
Bro Faz SolBro Faz Sol
314
314
It seems you'll need<span>
elements for each letter you'd like to color.
– ggorlen
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
CC_List1
are incorrect, the correct islist
. I will edit the post, sorry.
– Bro Faz Sol
4 hours ago
2
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago
add a comment |
It seems you'll need<span>
elements for each letter you'd like to color.
– ggorlen
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
CC_List1
are incorrect, the correct islist
. I will edit the post, sorry.
– Bro Faz Sol
4 hours ago
2
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago
It seems you'll need
<span>
elements for each letter you'd like to color.– ggorlen
4 hours ago
It seems you'll need
<span>
elements for each letter you'd like to color.– ggorlen
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
CC_List1
are incorrect, the correct is list
. I will edit the post, sorry.– Bro Faz Sol
4 hours ago
CC_List1
are incorrect, the correct is list
. I will edit the post, sorry.– Bro Faz Sol
4 hours ago
2
2
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago
add a comment |
5 Answers
5
active
oldest
votes
If result is a text then you need to wrap each letter with span..
do like code below:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
$("#list")
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<b class='colorful_text'></b>" );
$.each($(".colorful_text"), function(o, v){
var textEle = $(this).text()
console.log("textEle", textEle)
$(this).html("")
for(var n=0; n<textEle.length; n++) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var color = '#' + randomColor
var ele = document.createElement("span")
ele.style.color = color
ele.innerText = textEle[n]
$(this).append(ele)
}
})
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$("#list").html();
//Here should call a function to color all the words of the div
}
});
</script>
Worked perfectly! The only problem is that you are returning the<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and<br>
function as a line break?
– Bro Faz Sol
3 hours ago
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
add a comment |
If you want to get only vivid colors, using randomness like this wont do.
What you need is a function for HSV to RGB conversion, like this:
function HSVtoRGB(h, s, v)
{
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255).toString(16);
g = Math.round(g * 255).toString(16);
b = Math.round(b * 255).toString(16);
if (r.length < 2) r = '0' + r;
if (g.length < 2) g = '0' + g;
if (b.length < 2) b = '0' + b;
return '#' + r + g + b;
}
Parameters range from 0 to 1. Using HSVtoRGB(Math.random(), 1, 1)
will always yield you a vivid color with maximum value and saturation.
add a comment |
You would have to break your text into children spans
and do the coloring on them.
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
add a comment |
ok, I try this, it exactly working like you are asking. You can use it in your code.
window.setInterval(function(){
changeLetterColor();
}, 3000);
function changeLetterColor(){
var string = "hello world";
var customstring ='';
for(var i =0;i<string.length;i++){
customstring += "<span font color= '#"+Math.floor(100000 + Math.random() * 900000)+"'>"+ string[i]+" </span>";
}
}
add a comment |
change color after every 3 seconds :
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54248281%2frandom-colorful-letters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
If result is a text then you need to wrap each letter with span..
do like code below:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
$("#list")
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<b class='colorful_text'></b>" );
$.each($(".colorful_text"), function(o, v){
var textEle = $(this).text()
console.log("textEle", textEle)
$(this).html("")
for(var n=0; n<textEle.length; n++) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var color = '#' + randomColor
var ele = document.createElement("span")
ele.style.color = color
ele.innerText = textEle[n]
$(this).append(ele)
}
})
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$("#list").html();
//Here should call a function to color all the words of the div
}
});
</script>
Worked perfectly! The only problem is that you are returning the<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and<br>
function as a line break?
– Bro Faz Sol
3 hours ago
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
add a comment |
If result is a text then you need to wrap each letter with span..
do like code below:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
$("#list")
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<b class='colorful_text'></b>" );
$.each($(".colorful_text"), function(o, v){
var textEle = $(this).text()
console.log("textEle", textEle)
$(this).html("")
for(var n=0; n<textEle.length; n++) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var color = '#' + randomColor
var ele = document.createElement("span")
ele.style.color = color
ele.innerText = textEle[n]
$(this).append(ele)
}
})
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$("#list").html();
//Here should call a function to color all the words of the div
}
});
</script>
Worked perfectly! The only problem is that you are returning the<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and<br>
function as a line break?
– Bro Faz Sol
3 hours ago
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
add a comment |
If result is a text then you need to wrap each letter with span..
do like code below:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
$("#list")
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<b class='colorful_text'></b>" );
$.each($(".colorful_text"), function(o, v){
var textEle = $(this).text()
console.log("textEle", textEle)
$(this).html("")
for(var n=0; n<textEle.length; n++) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var color = '#' + randomColor
var ele = document.createElement("span")
ele.style.color = color
ele.innerText = textEle[n]
$(this).append(ele)
}
})
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$("#list").html();
//Here should call a function to color all the words of the div
}
});
</script>
If result is a text then you need to wrap each letter with span..
do like code below:
<div id="list" rows="10"></div>
<script>
$(function() {
setTime();
function setTime() {
$.ajax({
url : "../abc.php",
dataType: "text",
success : function (result) {
$("#list").html(result);
$("#list")
.contents()
.filter(function(){
return this.nodeType !== 1;
})
.wrap( "<b class='colorful_text'></b>" );
$.each($(".colorful_text"), function(o, v){
var textEle = $(this).text()
console.log("textEle", textEle)
$(this).html("")
for(var n=0; n<textEle.length; n++) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
var color = '#' + randomColor
var ele = document.createElement("span")
ele.style.color = color
ele.innerText = textEle[n]
$(this).append(ele)
}
})
}
});
var date = new Date().getTime();
setTimeout(setTime, 3000);
$("#list").html();
//Here should call a function to color all the words of the div
}
});
</script>
edited 2 hours ago
answered 4 hours ago
anggito wibisonoanggito wibisono
444
444
Worked perfectly! The only problem is that you are returning the<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and<br>
function as a line break?
– Bro Faz Sol
3 hours ago
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
add a comment |
Worked perfectly! The only problem is that you are returning the<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and<br>
function as a line break?
– Bro Faz Sol
3 hours ago
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
Worked perfectly! The only problem is that you are returning the
<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and <br>
function as a line break?– Bro Faz Sol
3 hours ago
Worked perfectly! The only problem is that you are returning the
<br>
of the file as a string and not as a tag. See: prntscr.com/m8qz05. What can I do to fix this and <br>
function as a line break?– Bro Faz Sol
3 hours ago
1
1
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
i edited my answer and do some tricks.
– anggito wibisono
2 hours ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
Thank you! Perfect!
– Bro Faz Sol
1 hour ago
add a comment |
If you want to get only vivid colors, using randomness like this wont do.
What you need is a function for HSV to RGB conversion, like this:
function HSVtoRGB(h, s, v)
{
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255).toString(16);
g = Math.round(g * 255).toString(16);
b = Math.round(b * 255).toString(16);
if (r.length < 2) r = '0' + r;
if (g.length < 2) g = '0' + g;
if (b.length < 2) b = '0' + b;
return '#' + r + g + b;
}
Parameters range from 0 to 1. Using HSVtoRGB(Math.random(), 1, 1)
will always yield you a vivid color with maximum value and saturation.
add a comment |
If you want to get only vivid colors, using randomness like this wont do.
What you need is a function for HSV to RGB conversion, like this:
function HSVtoRGB(h, s, v)
{
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255).toString(16);
g = Math.round(g * 255).toString(16);
b = Math.round(b * 255).toString(16);
if (r.length < 2) r = '0' + r;
if (g.length < 2) g = '0' + g;
if (b.length < 2) b = '0' + b;
return '#' + r + g + b;
}
Parameters range from 0 to 1. Using HSVtoRGB(Math.random(), 1, 1)
will always yield you a vivid color with maximum value and saturation.
add a comment |
If you want to get only vivid colors, using randomness like this wont do.
What you need is a function for HSV to RGB conversion, like this:
function HSVtoRGB(h, s, v)
{
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255).toString(16);
g = Math.round(g * 255).toString(16);
b = Math.round(b * 255).toString(16);
if (r.length < 2) r = '0' + r;
if (g.length < 2) g = '0' + g;
if (b.length < 2) b = '0' + b;
return '#' + r + g + b;
}
Parameters range from 0 to 1. Using HSVtoRGB(Math.random(), 1, 1)
will always yield you a vivid color with maximum value and saturation.
If you want to get only vivid colors, using randomness like this wont do.
What you need is a function for HSV to RGB conversion, like this:
function HSVtoRGB(h, s, v)
{
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6)
{
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
r = Math.round(r * 255).toString(16);
g = Math.round(g * 255).toString(16);
b = Math.round(b * 255).toString(16);
if (r.length < 2) r = '0' + r;
if (g.length < 2) g = '0' + g;
if (b.length < 2) b = '0' + b;
return '#' + r + g + b;
}
Parameters range from 0 to 1. Using HSVtoRGB(Math.random(), 1, 1)
will always yield you a vivid color with maximum value and saturation.
answered 4 hours ago
HavenardHavenard
17.9k22646
17.9k22646
add a comment |
add a comment |
You would have to break your text into children spans
and do the coloring on them.
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
add a comment |
You would have to break your text into children spans
and do the coloring on them.
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
add a comment |
You would have to break your text into children spans
and do the coloring on them.
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
You would have to break your text into children spans
and do the coloring on them.
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
function colorElement(element) {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
element.style.color = '#' + randomColor;
}
function splitElement(element) {
var text = element.innerText;
element.innerText = '';
var chars = text.split('');
for(var ch of chars) {
var charSpan = document.createElement('span');
charSpan.innerText = ch;
element.appendChild(charSpan);
}
}
function randomColor() {
var result = document.querySelectorAll('span span');
for(ele of result){
colorElement(ele);
}
}
var ele = document.getElementById('myText');
splitElement(ele);
setInterval(function() {
randomColor();
},500);
<div>
<span id="myText">Disco Text</span>
</div>
answered 3 hours ago
Aditya GuptaAditya Gupta
6991720
6991720
add a comment |
add a comment |
ok, I try this, it exactly working like you are asking. You can use it in your code.
window.setInterval(function(){
changeLetterColor();
}, 3000);
function changeLetterColor(){
var string = "hello world";
var customstring ='';
for(var i =0;i<string.length;i++){
customstring += "<span font color= '#"+Math.floor(100000 + Math.random() * 900000)+"'>"+ string[i]+" </span>";
}
}
add a comment |
ok, I try this, it exactly working like you are asking. You can use it in your code.
window.setInterval(function(){
changeLetterColor();
}, 3000);
function changeLetterColor(){
var string = "hello world";
var customstring ='';
for(var i =0;i<string.length;i++){
customstring += "<span font color= '#"+Math.floor(100000 + Math.random() * 900000)+"'>"+ string[i]+" </span>";
}
}
add a comment |
ok, I try this, it exactly working like you are asking. You can use it in your code.
window.setInterval(function(){
changeLetterColor();
}, 3000);
function changeLetterColor(){
var string = "hello world";
var customstring ='';
for(var i =0;i<string.length;i++){
customstring += "<span font color= '#"+Math.floor(100000 + Math.random() * 900000)+"'>"+ string[i]+" </span>";
}
}
ok, I try this, it exactly working like you are asking. You can use it in your code.
window.setInterval(function(){
changeLetterColor();
}, 3000);
function changeLetterColor(){
var string = "hello world";
var customstring ='';
for(var i =0;i<string.length;i++){
customstring += "<span font color= '#"+Math.floor(100000 + Math.random() * 900000)+"'>"+ string[i]+" </span>";
}
}
answered 4 hours ago
Sayed Mohd AliSayed Mohd Ali
1,0121318
1,0121318
add a comment |
add a comment |
change color after every 3 seconds :
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
add a comment |
change color after every 3 seconds :
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
add a comment |
change color after every 3 seconds :
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
change color after every 3 seconds :
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
var str = 'This is test' ;
var len = str.length ;
var selectedColor = ;
var result = document.getElementById('result') ;
function changeColor() {
selectedColor = ;
for( var i = 0; i < len; i++ )
selectedColor.push(Math.floor(Math.random()*16777215).toString(16) ) ;
return Array.from(str).reduce(function (acc, curr, index) {
acc += '<span style=color:#' + selectedColor[index] + '>' + curr + '</span>' ;
return acc ;
},'') ;
}
setInterval(function(){
result.innerHTML = changeColor() ;
},3000)
#result { font-size : 70px }
<p id="result"></p>
answered 1 hour ago
EhsanEhsan
9,66931129
9,66931129
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54248281%2frandom-colorful-letters%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e) {
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom)) {
StackExchange.using('gps', function() { StackExchange.gps.track('embedded_signup_form.view', { location: 'question_page' }); });
$window.unbind('scroll', onScroll);
}
};
$window.on('scroll', onScroll);
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems you'll need
<span>
elements for each letter you'd like to color.– ggorlen
4 hours ago
what's CC_List1 can you show some html
– priyanshi srivastava
4 hours ago
CC_List1
are incorrect, the correct islist
. I will edit the post, sorry.– Bro Faz Sol
4 hours ago
2
I don’t think you can apply style to text nodes; only on elements. I suppose you can break apart the letters into individual spans...
– alans
4 hours ago