1 reply [Last post]
aka.snowball
aka.snowball's picture
Offline
newbie
Last seen: 11 years 3 weeks ago
Timezone: GMT+2
Joined: 2010-06-07
Posts: 8
Points: 14

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 Smile

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;
}

Tags:
Chris..S
Chris..S's picture
Offline
Moderator
Last seen: 8 years 45 weeks ago
Timezone: GMT+1
Joined: 2005-02-22
Posts: 6078
Points: 173

Do you need that many

Do you need that many backslashes?
Are any white space characters besides space legal?

My function was:

function getElementsByClass(e, className, tagName) {
  var result = new Array;
  if (!e) return result;
 
  if (!tagName) tagName = '*';
 
  var pattern = new RegExp ("(^| )"+className+"( |$)");
  var nodes = e.getElementsByTagName(tagName);
 
  for (var i=0; i<nodes.length; i++) {
    if (nodes[i].className && nodes[i].className.match(pattern)) {
      result.push(nodes[i]);
    }
  }
 
  return result;
}

Was, because I use jquery now. There is no need to invent my own wheel when someone else has invented a far better one.