Suppose I have the following JavaScript code:
<style type="text/css">
#box1{
position: absolute;
top: 20px;
left: 50px;
width: 200px;
border: solid #ff0000 3px;
background-color: #ffff00;
color: #000000;
padding: 10px;
}
</style>
<script language="JavaScript">
function change(){
document.getElementById("box1".style.borderColor="#0000FF";
document.getElementById("box1".style.backgroundColor="#000000";
document.getElementById("box1".style.color="#FFFFFF";
}
</script>
<div id="box1" onMouseOver="change()">
<h3>GOOD MORNING</h3>
As such, the above code works fine but instead of using getElementById (the part of the code shown in brown), the same effect can be given using the following code:
<script language="JavaScript">
function change(){
box1.style.borderColor="#0000FF";
box1.style.backgroundColor="#000000";
box1.style.color="#FFFFFF";
}
</script>
Then what for is getElementById used in the first code? Under what circumstances should getElementById be used?
Thanks,
Arpan
<style type="text/css">
#box1{
position: absolute;
top: 20px;
left: 50px;
width: 200px;
border: solid #ff0000 3px;
background-color: #ffff00;
color: #000000;
padding: 10px;
}
</style>
<script language="JavaScript">
function change(){
document.getElementById("box1".style.borderColor="#0000FF";
document.getElementById("box1".style.backgroundColor="#000000";
document.getElementById("box1".style.color="#FFFFFF";
}
</script>
<div id="box1" onMouseOver="change()">
<h3>GOOD MORNING</h3>
As such, the above code works fine but instead of using getElementById (the part of the code shown in brown), the same effect can be given using the following code:
<script language="JavaScript">
function change(){
box1.style.borderColor="#0000FF";
box1.style.backgroundColor="#000000";
box1.style.color="#FFFFFF";
}
</script>
Then what for is getElementById used in the first code? Under what circumstances should getElementById be used?
Thanks,
Arpan