Need to improve my Getelementbyclassname

Hi

I have the following getelementbyclassname

function hideDisplayTestsuites(textstring1, textstring2, tag) {
var myclass1 = new RegExp('\\\\b'+textstring1+'\\\\b');
if(textstring2 !=null){
var myclass2 = new RegExp('\\\\b'+textstring2+'\\\\b');
}

//Populate the array with all the page tags
var allPageTags=document.getElementById('test-results').getElementsByTagName("*");
//Cycle through the tags using a for loop
for (i=0; i<allPageTags.length; i++) {
//Pick out the tags with our class name
if (myclass1.test(allPageTags[i].className) || myclass2.test(allPageTags[i].className)) {


if (tag == 'hide'){
	allPageTags[i].style.display = 'none';
}
else{
allPageTags[i].style.display = '';
}



}
}
}



problem is that it is a little slow implementation, so anybody have another faster implementation?I searched the web for jquery version of this, but coudnt find a logical exmplenation on how to use it. So if anyone sits on a link to a page where they describe how to do this in jquery I would be happy to recieve it :slight_smile:

You can have global variable, but in jQuery when you add # in front of text than you get only one element with that id,
but if you use it without for example $(‘div’) than you get all divs on page, but if you want to get all divs with particular class


//if we are searching for divs with class=test
$('div.test').each(function(){
   //your code
   //example
     $('this').css({display: 'none'});
});

enjoj

So is this the syntax for ids

var coverageRows = $('#coverage');

And why cant i dont have global variable? seems like a waste of time to declare it everytime i call the function.

I can’t have a global variable?

function showallCoverage(){
var coverageRows = $(‘.coverage-row’);
coverageRows.show();
}
You have forgotten the dot :slight_smile: and added a global var

Does that work the same for classes, and “id’s”? like var ok-testcases = $(‘ok-testcases’);?

Say that I want to show all objects with the class coverage-row.

Tried this, but don’t think its the way to go

var coverageRows = $('coverage-row');
function showallCoverage(){
coverageRows.show();
}

This is very easy in jQuery:


var divs = $('div');

divs will now contain a jQuery collection of all divs.


var divs = $('div');
divs.hide();

This will hide all the divs in the document.