I'm having issues Looping through elements on a form using a simple 'getElementsByClass' function. I have to use this heading:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
I also have to use IE8.
If I remove the heading, the function works fine. I realize the errant tags are not in the correct spots, but if I remove them, the function works correctly as well. There is much more to the page, but here is a stripped down version:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function getElementsByClass(node,searchClass,tag) {
var classElements = new Array();
var els = node.getElementsByTagName(tag); // use "*" for all elements
var elsLen = els.length;
alert("elsLen: " + elsLen);
var pattern = new RegExp("\\b"+searchClass+"\\b");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
alert("getElementsByClass: classElements.length: " + classElements.length);
return classElements;
}
</script>
</head>
<body>
<table>
<form name="formOne">
<input type="button" value="click" onclick="getElementsByClass(document.formOne,'popupElement','*');" />
<input type="text" class="popupElement">
</form>
<form name="formTwo">
<table>
<input type="text" class="popupElement">
<input type="text" class="popupElement">
<input type="button" value="click2" onclick="getElementsByClass(document.formTwo,'popupElement','*');" />
</form>
The first call to getElementsByClass() on formOne fires correctly and displays the proper values on the alert box. But on the call on formTwo, the function does not find any elements on the form.
I'm just trying to figure out why exactly this happens so I can develop a workaround.