Wednesday, September 21, 2011

How do I change font color dynamically using a HTA (HTML Application)?

Is there a way to change the font color of text in my hta dynamically? In my hta I have a div and I'd like to change the font color of the text in that div in the code. Is there a way to do this?





example:





%26lt;div id=';myDiv';%26gt;Some text%26lt;/div%26gt;





Is there some property I can change along the lines of





Sub ChangeColor


myDiv.color = ';red';


End SubHow do I change font color dynamically using a HTA (HTML Application)?
You appear to be writing a script in vbscript. I think what you want is something like:





%26lt;!DOCTYPE html PUBLIC ';-//W3C//DTD HTML 4.01 Transitional//EN';


';http://www.w3.org/TR/html4/loose.dtd'; %26gt;


%26lt;html lang='en'%26gt;


%26lt;head%26gt;


%26lt;meta http-equiv='Content-type' content='text/html;charset=UTF-8'%26gt;


%26lt;title%26gt;Change Colour%26lt;/title%26gt;


%26lt;style type=';text/css';%26gt;


div#myDiv {


color: blue;


}


%26lt;/style%26gt;


%26lt;script type=';text/vbscript';%26gt;


Function ChangeColour(colour)


document.getElementById( ';myDiv'; ).style.color = colour


End Function


%26lt;/script%26gt;


%26lt;/head%26gt;


%26lt;body%26gt;


%26lt;h1%26gt;Change Colour%26lt;/h1%26gt;


%26lt;div id=';myDiv';%26gt;Some text%26lt;/div%26gt;





%26lt;input type=';button'; onclick=';ChangeColour('red')'; value=';Red';%26gt;


%26lt;input type=';button'; onclick=';ChangeColour('green')'; value=';Green';%26gt;


%26lt;input type=';button'; onclick=';ChangeColour('blue')'; value=';Blue';%26gt;


%26lt;/body%26gt;


%26lt;/html%26gt;





However this will only work with IE.





To work with all browsers you would need to use javascript. In the above you could replace the script with the functionally equivalent:





%26lt;script type=';text/javascript';%26gt;


function ChangeColour(colour) {


document.getElementById( ';myDiv'; ).style.color = colour;


}


%26lt;/script%26gt;





For more on the HTML script tag see: http://www.html-tags-guide.com/html-scri鈥?/a>





[Edit]





In terms of your Additional Details:





Rather than:





myDiv.innerhtml=';%26lt;font color='red'%26gt;Some text.%26lt;/font%26gt;';





You could try:





myDiv.style.color = ';red';How do I change font color dynamically using a HTA (HTML Application)?
Thanks Michael, great answer.

Report Abuse



What you would need to do is to use the style tag of the div





e.g.





%26lt;div style=';color:#00FF00';%26gt;


%26lt;h3%26gt;This is a header%26lt;/h3%26gt;


%26lt;p%26gt;This is a paragraph.%26lt;/p%26gt;


%26lt;/div%26gt;

No comments:

Post a Comment