SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)

    Remove <li>'s with a custom attribute value

    I have a number of <li> elements that look similar to this:

    Code:
    <li id="post-142511" class="message  " data-author="author1">
    I need to be able to remove the one's where data-author="author1", (there is more than one).

    Any ideas on the most efficient way to do this?

  2. #2
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Presumably you're not worried that "data-author" won't validate as a known attribute. This isn't tested but you can try it, otherwise it may be necessary to use getAttribute.
    Code:
    var allItems = document.getElementsByTagName( 'LI' );
    
    for( var i = 0, anItem; ( anItem = allItems[ i ] ); i++ )
     if( anItem[ 'data-author' ] )
      anItem.parentNode.removeChild( anItem );
    Tab-indentation is a crime against humanity.

  3. #3
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    Presumably you're not worried that "data-author" won't validate as a known attribute. This isn't tested but you can try it, otherwise it may be necessary to use getAttribute.
    Code:
    var allItems = document.getElementsByTagName( 'LI' );
    
    for( var i = 0, anItem; ( anItem = allItems[ i ] ); i++ )
     if( anItem[ 'data-author' ] )
      anItem.parentNode.removeChild( anItem );
    Thanks. With the getAttribute I need to be able to specify what the value of data-author is. So if data-author = "author2" I do not want to remove it. If it's "author1", I do.

  4. #4
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hartmann View Post
    Thanks. With the getAttribute I need to be able to specify what the value of data-author is. So if data-author = "author2" I do not want to remove it. If it's "author1", I do.
    OK I missed that part so try this, which will be case-sensitive:
    Code:
    var allItems = document.getElementsByTagName( 'LI' );
    
    for( var i = 0, anItem; ( anItem = allItems[ i ] ); i++ )
     if( anItem[ 'data-author' ] == 'author1' )
      anItem.parentNode.removeChild( anItem );
    Tab-indentation is a crime against humanity.


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
  •