SitePoint Sponsor

User Tag List

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

    Problem to retrieve table rows with multiple classes

    The following works if the row that i want to display has one class. But if it has two classes this don't work.

    for example this works

    Code:
    <tr class="testcase-failed-message">
    <th>
    testcase-failed-message
    </th>
    <th>
    testcase-failed-message
    </th>
    </tr>
    And this don't since it has two classes. How can i have two classes on a tag and still chose wich one i want to use when i want to show them or not??


    Code:
    <tr class="testcase-failed-message failed-skipped">
    <th>
    testcase-failed-message
    </th>
    <th>
    testcase-failed-message
    </th>
    </tr>





    Code:
    function show(){
    doShow('testcase-failed-message');
    }
    
    
    
    
    function doShow(textstring) {
    
    var allPageTags=document.getElementsByTagName("*");
    
    for (i=0; i<allPageTags.length; i++) {
    
    if (allPageTags[i].className==textstring || allPageTags[i].className==textstring2) {
    
    allPageTags[i].style.display='none';
    }
    
    
    }
    }

  2. #2
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,784
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You need to use a regular expression that tests if the string contains the class name with a boundary on either side rather than just testing if it is equal.

    For example:

    var myclass1 = new RegExp('\\b'+textstring+'\\b');
    var myclass2 = new RegExp('\\b'+textstring2+'\\b');

    and then

    if (myclass1.test(allPageTags[i].className) || myclass2.test(allPageTags[i].className))
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As well as felgall's suggestion you can also use the indexOf method to check if the className contains the string...e.g:

    if (allPageTags[i].className.indexOf(textString) != -1) {
    alert("contains this string");
    }

    indexOf will return -1 if the string isn't found.


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
  •