SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need to improve my Getelementbyclassname

    Hi

    I have the following getelementbyclassname

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

  2. #2
    SitePoint Wizard bronze trophy Immerse's Avatar
    Join Date
    Mar 2006
    Location
    Netherlands
    Posts
    1,661
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)
    This is very easy in jQuery:

    Code:
    var divs = $('div');
    divs will now contain a jQuery collection of all divs.

    Code:
    var divs = $('div');
    divs.hide();
    This will hide all the divs in the document.

  3. #3
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does that work the same for classes, and "id's"? like var ok-testcases = $('ok-testcases');?

  4. #4
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Say that I want to show all objects with the class coverage-row.

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


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

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function showallCoverage(){
    var coverageRows = $('.coverage-row');
    coverageRows.show();
    }
    You have forgotten the dot and added a global var

  6. #6
    SitePoint Member
    Join Date
    Jul 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So is this the syntax for ids
    Code:
    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?

  7. #7
    SitePoint Enthusiast TriLLi's Avatar
    Join Date
    Feb 2010
    Location
    BiH, Bugojno
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

    Code JavaScript:
    //if we are searching for divs with class=test
    $('div.test').each(function(){
       //your code
       //example
         $('this').css({display: 'none'});
    });
    enjoj
    http://www.wiseblog.info
    Programmers don't die, they just GO SUB without return.


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
  •