SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with getElementsByClass javascript

    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


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

  2. #2
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I suppose you are setting aryClassElements.length to zero because you only want to carry out the action on the first element....

    Without running the script, if you set the display of the elements you want to show to "block", can you see them. If not, then you are dealing with a CSS issue. To test give those elements a fixed width and height and rerun your script. If this works, you can then set your width and height to auto.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •