Change Text Color With Javascript? - Stack Overflow

    1. Home
    2. Questions
    3. Tags
    4. Users
    5. Companies
    6. Labs
    7. Jobs
    8. Discussions
    9. Collectives
    10. Communities for your favorite technologies. Explore all Collectives

  1. Teams

    Ask questions, find answers and collaborate at work with Stack Overflow for Teams.

    Try Teams for free Explore Teams
  2. Teams
  3. Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Get early access and see previews of new features.

Learn more about Labs Change text color with Javascript? Ask Question Asked 11 years, 5 months ago Modified 3 years, 2 months ago Viewed 216k times 25

I want to change the color of a title when a button is clicked. This is my code, but it's not working and I can't figure out why not...

var about; function init() { about = document.getElementById("about").innerHTML; about.style.color = 'blue'; } <div id="about">About Snakelane</div> <input type="image" src="http://www.blakechris.com/snakelane/assets/about.png" onclick="init()" id="btn">

Share Improve this question Follow edited Oct 19, 2021 at 7:30 Tomerikoo's user avatar Tomerikoo 19.4k16 gold badges53 silver badges67 bronze badges asked Jul 29, 2013 at 13:38 user2375017user2375017 Add a comment |

4 Answers 4

Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 47

You set the style per element and not by its content:

function init() { document.getElementById("about").style.color = 'blue'; }

With innerHTML you get/set the content of an element. So if you would want to modify your title, innerHTML would be the way to go.

In your case, however, you just want to modify a property of the element (change the color of the text inside it), so you address the style property of the element itself.

Share Improve this answer Follow answered Jul 29, 2013 at 13:40 Sirko's user avatar SirkoSirko 74k19 gold badges153 silver badges189 bronze badges Add a comment | 8

use ONLY

function init() { about = document.getElementById("about"); about.style.color = 'blue'; }

.innerHTML() sets or gets the HTML syntax describing the element's descendants., All you need is an object here.

Demo

Share Improve this answer Follow edited Feb 18, 2014 at 18:49 animuson's user avatar animuson 54.7k28 gold badges141 silver badges150 bronze badges answered Jul 29, 2013 at 13:40 Dhaval Marthak's user avatar Dhaval MarthakDhaval Marthak 17.4k6 gold badges47 silver badges69 bronze badges Add a comment | 4

Try below code:

$(document).ready(function(){ $('#about').css({'background-color':'black'}); });

http://jsfiddle.net/jPCFC/

Share Improve this answer Follow answered Jul 29, 2013 at 13:45 Nitin Chaurasia's user avatar Nitin ChaurasiaNitin Chaurasia 2631 silver badge5 bronze badges 1
  • 4 No mentioning of jQuery anywhere in the question. The function also is in the onclick handler of the <input> element and not in some onload event of the document. – Sirko Commented Jul 29, 2013 at 13:51
Add a comment | 0

innerHTML is a string representing the contents of the element.

You want to modify the element itself. Drop the .innerHTML part.

Share Improve this answer Follow answered Jul 29, 2013 at 13:40 Quentin's user avatar QuentinQuentin 942k132 gold badges1.3k silver badges1.4k bronze badges Add a comment | Highly active question. Earn 10 reputation (not counting the association bonus) in order to answer this question. The reputation requirement helps protect this question from spam and non-answer activity.
  • The Overflow Blog
  • You should keep a developer’s journal
  • The real 10x developer makes their whole team better
  • Featured on Meta
  • The December 2024 Community Asks Sprint has been moved to March 2025 (and...
  • Stack Overflow Jobs is expanding to more countries

Linked

-2 I selected an element by title with JavaScript, how can I update the css? -1 JQUERY how to change the color of a circle in turn with the same button 3 How to change the text color with button click? 1 How to change color of a piece of text in Javascript? 0 Changing font colour of string value 0 change color of a text in html using javascript 10 Changing text color onclick 1 Javascript: on click change button text color 3 How to change the text color with button click? 0 How to change text and color of a button when it is clicked using Javascript/HTML only 2 Change text color when clicking button in Javascript 2 Change font color via button click 0 Change text colors by clicking on button with javascript 0 Button that assigns different color to text in javascript 0 How to Update button text with Javascript 0 How do I change the color of a text with a button in Java Script?

Hot Network Questions

  • Space Shuttle HUD use outside of landing?
  • Can the Turing barrier be behing the AI (neural) training wall and hence - there is no chance to break the AI wall?
  • What kind of cosmic event could justify the entire Solar System being uninhabitable in the near future (1000 years or less)?
  • Retro PC: How to Install Windows XP to SSD Using PATA to SATA Adapter On ICH1 Through ICH6
  • To prove that the roots of a quadratic equation aren't real using real number system.
  • Is it a good idea to immerse the circuit in an engineered fluid in order to minimize circuit drift
  • How energy conservation works in conserved angular momentum scenerio?
  • What livery is on this F-5 airframe?
  • Can I use bootstrapping for small sample sizes to satisfy the power analysis requirements?
  • Intermediate disc efficiency with induction stove
  • Why do most philosophers of religion believe in God?
  • Liquefaction of gases in the absence of gravity
  • Do you lose the right of attribution if you're charged with a crime?
  • Hooking backspace character
  • What word(s) were used to identify the Van Dyke style of beard in the 17th century?
  • Would the poulterer's be open on Christmas Day for Scrooge to buy their prize turkey?
  • CD with physical hole is perfectly readable - how?
  • How much influence do the below 3 SCOTUS precedents have for Trump voiding birthright citizenship?
  • Debian Bookworm always sets `COLUMNS` to be a little less than the actual terminal width
  • Improve traction on icy path to campsite
  • Double factorial power series closed form expression
  • Where did Tolstoy write that a man is like a fraction?
  • What does the verb advantage mean in this sentence from chapter one of "Wuthering Heights"?
  • How to obtain Cyrillic letters with polyglossia and main font TeX Gyre Pagella?
more hot questions Question feed Subscribe to RSS Question feed

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

lang-js

Từ khóa » Html Color Text Javascript