Forum : Element get by Class
Brief description  about Online courses   join in Online courses
View Santosh  Hegde 's Profile

Element get by Class


Dear Sir,
i recently coded here, having problem that, how can i get Class insted of document.getElementById().value?

code:

<html>
<head>
<title>Onblur Onchange Functions</title>

<style type ="text/css">
.onfocus
{
border:2px solid #306EFF;
height: 23px;
width: 148px;
padding: 3px 5px 0 5px;
background:none;
font: normal 13px Arial, Helvetica, sans-serif;
}
.onblur
{
border:1px solid #041e33;
height: 22px;
width:147px;
padding: 3px 5px 0 5px;
background:none;
font: normal 13px Arial, Helvetica, sans-serif;
}
.fname {
width: 167px;
height: 20px;
padding: 3px 5px 0 5px;
margin: 3px 0 5px 10px;
background:none;
font: normal 11px Arial, Helvetica, sans-serif;
border: 1px solid #041e33;
color: #34312C;
}
</style>

<script type="text/javascript">
function upperCase(obj, evt)
{
var x=document.getElementById("fname").value;
document.getElementById("fname").value=x.toUpperCase();

if(evt.type=="focus")
obj.className ="onfocus";
else if(evt.type=="blur")
obj.className ="onblur";

}
</script>
</head>

<body>
Enter your name: <input type="text" id="fname" style="color: #34312C;" onfocus="upperCase(this, event)"onblur="upperCase(this, event)" />
<br />
<br />
Enter your age: <input type="text" id="age" onblur="alert(this.id)" />
<br />


</body>

</html>

in the case i need to get first text box default first thats controlled by css class. and onfocus event want to change border color. in onblur event it should be appear to default. it last two event working
i tried write like :
var x=document.getElementByClassName("fname").value;
but in this way its not working

so please guide me...

Thanks
Santosh Hegde
Asked by Santosh Hegde | Jan 25, 2011 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
Hi Santosh,

U can change the class attribute with setAttribute(),

document.getElementById("idElement").setAttribute("class", "myClass");

check this. This ll work for u.. according to condition change the classes
Jan 31, 2011