Function – getID

Javascript Function – getID
What does it do?
Regardless of what web browser is used, the function will return the correct javascript used to manipulate tags on the page. For example, you want to change the class of a div tag on the page, as long as you give the div an id you can use this function to change the className of the div.
Code
To be placed in between your head tags
<script type="text/javascript">
function getID(id)
{
var temp
if (document.getElementById)
{
temp = document.getElementById(id);
} else if (document.all) {
temp = document.all[id];
} else {
temp = document.getElementById(id);
}
return temp;
}
</script>
Usage
getID(id_string)
Example
I have a div that i’d like to toggle it’s visibility on and off by clicking a link.
Code
Javascript:
function getID(id)
{
var temp
if (document.getElementById)
{
temp = document.getElementById(id);
} else if (document.all) {
temp = document.all[id];
} else {
temp = document.getElementById(id);
}
return temp;
}
function toggleVisible(id)
{
if (getID(id).className == "show")
{
getID(id).className = "hide";
} else if (getID(id).className == "hide") {
getID(id).className = "show";
}
}
CSS:
.show { display: block; }
.hide { display: none; }
#test_div
{
text-align: center;
background-color: #F8F8F8;
padding: 20px;
}
HTML:
<div><a href="javascript: toggleVisible('test_div');">Show/Hide DIV</a></div>
<div id="test_div" class="hide">This is a test</div>
Demo
This is a test
Other Examples
Hiding a div is only one of many things you could do with this, some other examples are:
Changing the src of an image:
getID('image_id').src = 'newimage.gif';
Return the value of a form field
getID('textbox_id').value
