SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2004
    Location
    Farmington
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    DOM manipulation

    I need help with the following using javascript or jQuery.

    I need to perform the following operations on a string like the below. There are multiple like this on the page and the main identifier is "ms-gb" class on the td tag.

    <tbody id="titl4909-1_" groupString="%3B%23BOE%204%2E0%20Shop%20Cost%20Study%3B%23"><tr id="group0"><td colspan="100" nowrap class="ms-gb"><a href="javascript:" onclick="javascript:ExpCollGroup('4909-1_', 'img_4909-1_',event, false);return false;"><img src="/_layouts/images/minus.gif" border="0" alt="collapse" id="img_4909-1_" />&nbsp;L1</a> : My Test File Name<span style="font-weight: lighter">&nbsp;‎(3)</span></td></tr></tbody>

    1.If the text in between closing anchor tag "</a>" and beginning span tag "<span" is just 3 characters long (that is " : "), I would like to remove the whole parent tbody tag.

    Else, I would like to do the following

    2. I would like to replace "&nbsp;L1" between image close tag "/>" and anchor close tag "</a>". The text "&nbsp;L1" is not static and can be different.

    3. I would like to remove " : " which appears after closing anchor tag "</a>"

    4. I would like to remove the entire span tag.

  2. #2
    SitePoint Enthusiast Anthony.Barnes's Avatar
    Join Date
    Aug 2011
    Location
    Canberra, Australia
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this will get you part of the way there:

    Code JavaScript:
    // Get any "blank" rows
    var $blankNodes = jQuery('table tr').find('*').contents().filter(function(){
        return this.nodeType === 3 && (/^\s:\s$/.exec(this.nodeValue) !== null);
    });
     
    // Remove the tbody if the file name isn't present
    if ($blankNodes.length) {
       $blankNodes.parent().remove();
    }
     
    // We know the remaining nodes contain valid content now
    // Remove all spans
    jQuery('table tr span').remove();

    The other part you described about replacing content within the text nodes is a little trickier, you could use something similar to the method used to detect which nodes contained only ' : ', but adjust the regular expressions to find the patterns you know about.


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
  •