SitePoint Sponsor

User Tag List

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

    Problem with hiding and showing classes

    Hi I have the following code that don't work like i want it.

    Code:
    function hideDisplaySingleSuite(textstring) {
    var myclass1 = new RegExp('\\b'+textstring+'\\b');
    //Populate the array with all the page tags
    var allPageTags=document.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)) {
    if (allPageTags[i].style.display == ''){
    	allPageTags[i].style.display = 'none';
    }
    else{
    allPageTags[i].style.display = '';
    }
    
    
    
    }
    }
    }
    For example

    It show objects if I remove the if statement that checks if the class is visible.

    for example this shows a class and works.

    Code:
    function hideDisplaySingleSuite(textstring) {
    var myclass1 = new RegExp('\\b'+textstring+'\\b');
    //Populate the array with all the page tags
    var allPageTags=document.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)) {
    
    allPageTags[i].style.display = '';
    
    }
    }
    }

    So my question is why don't this work on a class, it works on an "id"?

    Code:
    if (allPageTags[i].style.display ==''){
    	allPageTags[i].style.display = 'none';
    }
    else{
    allPageTags[i].style.display = '';
    }
    I want to show the class if its not visible or hide it if it is visible.
    How can this be done?

    Thanks

  2. #2
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Too much work. Research cssRules or stylesheet changers. You no longer need to iterate through every element on your page
    Also, a stylesheet changer can be noscript as well if done with server side languages
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, so how can I check if a class is "display=='none'" or "display==''"?

  4. #4
    ♪♪ ♪ ♪ ♪ ♪♪ ♪ ♪♪ Markdidj's Avatar
    Join Date
    Sep 2002
    Location
    Bournemouth, South UK
    Posts
    1,551
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    http://www.javascriptkit.com/dhtmltu...rnalcss3.shtml

    although I would probably set up an array of classStates to remember it for me

    ClassStates=new Array(0,0,0)
    LiveScript: Putting the "Live" Back into JavaScript
    if live output_as_javascript else output_as_html end if

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2010
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow thanks.

    I have another quastion(sorry for not dropping this but i just want to know one thing about my function).

    Code:
    function hideDisplaySingleSuite(textstring) {
    var myclass1 = new RegExp('\\b'+textstring+'\\b');
    //Populate the array with all the page tags
    var allPageTags=document.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)) {
    /*if (allPageTags[i].style.display == '')
    	allPageTags[i].style.display='none';*/
    if(allPageTags[i].style.display == 'none')
    allPageTags[i].style.display='';
    else
    allPageTags[i].style.display='none';
    
    
    
    
    }
    }
    }
    I know whats the problem with the above function, both the if and the else statement is executed, meaning that first it shows the rows and them hides them, thats why i thought that it didn't work, but it does....kind of :P. Do you know how come?

    I have never before experienced this type of "error" where both an if and an else statement is executed :P


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
  •