Wednesday, September 21, 2011

DHTML (change font family)?

Anyone knows how to change the font family of words when you click the button?



I know you have to apply DHTML for this...



My codes..



%26lt;html%26gt;

%26lt;head%26gt;

%26lt;title%26gt;Dynamic Classes Example%26lt;/title%26gt;

%26lt;style%26gt;

.Verdana {font-family: Verdana; color: red}

.Arial {font-family: Arial; color: black}

%26lt;/style%26gt;

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

%26lt;!--

function changeClass () {

theClass = document.getElementById(';myPara';).classN?br>
if (theClass != ';Arial';) {

document.getElementById(';myPara';).classN?= ';Verdana';;

}

else document.getElementById(';myPara';).classN?= ';Arial';;

}

// --%26gt;

%26lt;/script%26gt;

%26lt;/head%26gt;



%26lt;body%26gt;

%26lt;form name=';form1';%26gt;

%26lt;p id=';myPara'; class=';small';%26gt;Click on the button to change the class for this text.%26lt;/p%26gt;

%26lt;input type=';button'; value=';Change Class'; onClick=';changeClass()'; /%26gt;

%26lt;/form%26gt;

%26lt;/body%26gt;

%26lt;/html%26gt;



I clicked on it, it changed to another font family. But when I clicked it again, the font family doesn't change anymore...What's wrong...?DHTML (change font family)?
your condition is very tricky, so let's consider that the class is equal to Arial, so the else section is performed so it stays at Arial, so instead replace the != by ==

No comments:

Post a Comment