getElementById Problems in asp.net
9 May 2008
When using asp.net with control containers or master pages, the ID of the DHTML controls rendered on the client look something like 'ctl00$cphMain$btnFilter', this means accessing the known id 'btnFilter' with getElementById doesn't work. There are some asp.net workarounds for this such as
document.getElementById("<%= btnFilter.ClientID %>");
The new function GetElementByTagAndPartialID which takes two parameters, the name of the tag such as 'input' or 'div' and the original ID. This function first gets a collection of all controls with the specified tag name on the page and then loops through them to return the first one that partially matches the ID given.
function GetElementByTagAndPartialID(tagname, partialID)
var elementsWithTag = document.getElementsByTagName(tagname);
var elementFound = null;
for (i=0; i<elementsWithTag.length; i++)
if (elementsWithTag[i].id.indexOf(partialID) > -1)
elementFound = elementsWithTag[i];
If the tag name is not known in advanced, one would have to loop through all DHTML controls on the page which would take a bit longer.