Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to change font properties, of subsection of page.

 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to change the font color and size of a text with a button click? I do not want to change the font size and color of the entire page just a section of it.

For example a sentence in a paragraph will change to red, while the rest stay black, or the font size of a word on a paragraph changes size, but the rest stay the same on a button click.

The problem I have is that the examples I read like:

document.bgColor = '#FFFFFF';
document.fgColor='#FF0000';

Affects the whole page, I cannot figure out how to change a small section of the page. Thanks.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use css classes


you have your div


and you have your javascript


Eric
 
Malith Yapa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there is a more convenient way.

Just get the appropiate paragraph element using the

method and change it's style objects properties.



this would actually change the paragraphs color to red on the run..

After that all you have to do is create a function with the above code and call it from the onklick event. Like this.


the " | " are for te greater than and smaller than signs. Javaranch doesn't support them
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64967
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Malith Yapa:
I think there is a more convenient way.


That is a much more inconvenient way. Using that approiach each and every element in the section would need to be iterated over in order to change the color.

Much better to make one class name change and affect the entire DOM sub-tree.
 
Fritz Largosa
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, your sample code works great! I kinda knew I would have to use div but, didn't know how to change its class name, until now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic