Change Text Color With Javascript? - Stack Overflow
Có thể bạn quan tâm
-
- Home
- Questions
- Tags
- Users
- Companies
- Labs
- Jobs
- Discussions
- Collectives
-
Communities for your favorite technologies. Explore all Collectives
- Teams
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Try Teams for free Explore Teams - Teams
-
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 CollectivesTeams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about TeamsGet 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 25I 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 19.4k16 gold badges53 silver badges67 bronze badges asked Jul 29, 2013 at 13:38 user2375017user2375017 Add a comment |4 Answers
Sorted by: Reset to default Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first) 47You 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 SirkoSirko 74k19 gold badges153 silver badges189 bronze badges Add a comment | 8use 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♦ 54.7k28 gold badges141 silver badges150 bronze badges answered Jul 29, 2013 at 13:40 Dhaval MarthakDhaval Marthak 17.4k6 gold badges47 silver badges69 bronze badges Add a comment | 4Try 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 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
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 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 javascriptRelated
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?
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
lang-jsTừ khóa » Html Color Text Javascript
-
Change Text Color On Click Using JavaScript | Bobbyhadz
-
JavaScript String Fontcolor() Method - W3Schools
-
HTML DOM Style Color Property - W3Schools
-
How To Change The Font Color Of A Text Using JavaScript?
-
How To Change Font Color Of HTML Element In JavaScript?
-
How To Change The Text Color In JavaScript - Reactgo
-
Text Color Change In Js Code Example - Code Grepper
-
JavaScript: String Fontcolor() Method - TechOnTheNet
-
ntcolor() - JavaScript - MDN Web Docs
-
- HTML: HyperText Markup Language | MDN"> - HTML: HyperText Markup Language | MDN
-
How To Change Text Color In HTML? - Top 3 Ways To Create Awesome ...
-
How To Change Text Color In Html - Javatpoint
-
How To Change Text Color On Click Using JavaScript?
-
How To Change Text Color In Javascript - YouTube