PDA

View Full Version : Need help with getElementsByClass javascript


aka.snowball
11th June 10, 02:21 PM
I have a problem with the code below.

The first time it runs it hides the class "testcase-failed", but it dosen't make the class "testcase-ok" visible.

Any one have any ideas how I can both hide "testcase-failed" and display "testcase-ok"?

Thanks :)


function Find_OK_Cases() {
aryClassElements.length = 0;
aryClassElements = getElementsByClass('testcase-failed');
for ( var i = 0; i < aryClassElements.length; i++ ) {
aryClassElements[i].style.display='none';
}

aryClassElements.length = 0; /*This*/
aryClassElements = getElementsByClass('testcase-ok');/*section*/
for ( var i = 0; i < aryClassElements.length; i++ ) {/*dont*/
aryClassElements[i].style.display='block'; /*work*/
}

}

function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}

chrishirst
11th June 10, 10:05 PM
what are the CSS Rules for the classes?

aka.snowball
15th June 10, 09:14 AM
The following works if the row that i want to display has one class. But if it has two classes this don't work.

for example this works

<tr class="testcase-failed-message">
<th>
testcase-failed-message
</th>
<th>
testcase-failed-message
</th>
</tr>

And this don't since it has two classes. How can i have two classes on a tag and still chose wich one i want to use when i want to show them or not??


<tr class="testcase-failed-message failed-skipped">
<th>
testcase-failed-message
</th>
<th>
testcase-failed-message
</th>
</tr>







function show(){
doShow('testcase-failed-message');
}




function doShow(textstring) {

var allPageTags=document.getElementsByTagName("*");

for (i=0; i<allPageTags.length; i++) {

if (allPageTags[i].className==textstring || allPageTags[i].className==textstring2) {

allPageTags[i].style.display='none';
}


}
}

chrishirst
15th June 10, 10:48 AM
You would have to test for the WHOLE class attribute string not just part of it, or convert the code into a regular expression match for partial matching.