Q+A - How to alternate text color using JavaScript

Published: 19 November 2013
on channel: Adam Khoury
24,634
192

Q: How do I color every second character of a string into red in javascript? for example: var a="This is a string". I want the first, the third character and so on to be colored in red.

A: Split the string into an array holding each character in the string. Loop over that array and use condition logic to see if the array index position is divisible by any number you wish. If it is then you alter the target element to your liking. Watch the video for code explanation.


Watch video Q+A - How to alternate text color using JavaScript online without registration, duration hours minute second in high quality. This video was added by user Adam Khoury 19 November 2013, don't forget to share it with your friends and acquaintances, it has been viewed on our site 24,634 once and liked it 192 people.