Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
119
rated 0 times [  121] [ 2]  / answers: 1 / hits: 188492  / 11 Years ago, mon, july 29, 2013, 12:00:00

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>




More From » button

 Answers
3

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.


[#76675] Saturday, July 27, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;