SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Select parent if it hasn't a certain class

    I understand that using

    Code:
    .parent(".class")
    I can select the parent of the current element if it (the parent) has a class named "class". How can I select parents that don't have a certain class? Also, how can I select parents that don't have more than one class? Does the .parent() method allow to do this?

  2. #2
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to add a class only to elements whose parents don't have a certain class to avoid conflicts.

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,192
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    You should be able to use something like

    Code JavaScript:
    if (!$(this).parent().hasClass('className')) {
        // Do something...
    }

  4. #4
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SgtLegend View Post
    You should be able to use something like

    Code JavaScript:
    if (!$(this).parent().hasClass('className')) {
        // Do something...
    }
    Yeah, I thought about this too, maybe it's the easiest way. I was just wondering if I could just use the .parent() method to avoid having an if statement.

  5. #5
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    .parent(':not(.class)')
    .parent(':not(.class1, .class2)')

  6. #6
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by CyberAlien View Post
    .parent(':not(.class)')
    .parent(':not(.class1, .class2)')
    Great, thank you very much!!


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
  •