• Twitter
  • Facebook
  • Google+
  • Instagram
  • Youtube

Thursday, April 17, 2008

Javascript to dynamically add styles and event to form Element.

Javascript to dynamically add styles and event to form Element
=====================================================

<HTML>
<HEAD>

<style type="text/css">
.OnFocus
{
background-color : gray;
}
.OnBlur
{
background-color : white;
}
</style>

<script language="javascript" type="text/javascript">

function SetStyle()
{
var elem = document.getElementById('frmMain').elements;

for(var i = 0; i < elem.length; i++)
{
// Assing style to each and every textbox on this page.
if (elem[i].type == "text")
{
elem[i].setAttribute("onfocus","this.className = 'OnFocus';");
elem[i].setAttribute("onblur","this.className = 'OnBlur';");
}
}
}

</script>
</HEAD>

<BODY onload="javascript:SetStyle()">
<form id="frmMain" >

<input type="text" value="Hello 1" id="text1" /> <br/>
<input type="text" value="Hello 1" id="text2" /><br/>
<input type="password" value="Hello 1" id="text1" /> <br/>

</form>
</BODY>
</HTML>

That's It !
Hope you will like it.

0 comments:

Contact

Get in touch with me


Adress/Street

12 Street West Victoria 1234 Australia

Phone number

+(12) 3456 789

Website

www.johnsmith.com