Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS To select a specific parent tag.

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 18 total)
  • Author
    Posts
  • #40752
    Yong
    Member

    Hi guys,

    I need to select a specific parent tags of a website I’m working on.
    I don’t know if there is a way to do it, but I’d like to ask here.

    Here is my sample source.

    I’d like to know if there is a way to select ONLY the li that has “apple” class children. It is a dynamic e-commerce website. The class “apple” could show up in multiple places and random order. So, I can’t use :nth-child(n) method.

    Thank you,

    #114042
    JohnMotylJr
    Participant

    I think i am overthinking your question because i believe its a relativly easy fix. You just want to style your class with the li before it.

    li .apple {
    /* whatever styles you want */
    }

    http://jsfiddle.net/john_motyl/Z7Hdm/

    is that what you are needing?

    #114045
    Yong
    Member

    I think that selects `` tag instead of the `

  • `. Am I right? I need to select and apply a style to `
  • `, not ``.

    Thanks,

#114046
Paulie_D
Member

The answer is that there is NO parent selector but if you only have one list item with a class of ‘apple’ then the above option will work.

Otherwise you will just have to get more specific.

#114047
Paulie_D
Member

li.apple

will only select list items with a class of ‘apple’

#114048
TheDoc
Member

You cannot work backwards in CSS. I remember reading about parent selectors coming down the pipe, but I’m betting we’re still a couple years away from having support on that.

The simple solution here is to put the class on the list item.

  • Apple Link
  • This gives you total control over that list item. If you *can’t* modify the markup, then the only way you’re going to be able to select the parent `

  • ` is through JS.
  • #114049
    Yong
    Member

    Thanks guys,

    No, I can’t modify the markup. So, I think I have to find a way through JS.

    Hope parent selectors coming soon. Don’t you guys think it would be useful?

    #114052
    Paulie_D
    Member

    If the list items will ONLY contain links then

    li a.apple

    would work

    On thinking about it, wouldn’t

    li *.apple

    select ANYTHING with a class of ‘apple’ inside a list?

    #114054
    JohnMotylJr
    Participant

    @paulie_d : Wasnt sure if you really wanted to know or not.. http://jsfiddle.net/john_motyl/Z7Hdm/2/

    #114061
    TheDoc
    Member

    @Paulie_D – the OP is trying to select the list item itself, not any child elements.

    #114064
    Paulie_D
    Member

    @TheDoc Yeah, but on thinking about that what is he proposing to DO with that li?

    It might be that he’s asking the wrong question :)

    #114070
    Yong
    Member

    @Paulie_D : My question is correct. I need to select and style the li. It is a dynamic e-commerce website and it’s little too complicated to explain here.

    #114069
    Senff
    Participant

    You can use jQuery, I’d say:

    $(‘.apple’).parent().addClass(‘highlight’);

    This will add a class “highlight” to the LI if it contains a child with class “apple”. Sounds kinda obvious, but the fact that it wasn’t suggested already makes me wonder if there’s something wrong with it…..?

    #114075
    Yong
    Member

    Hi, Senff.

    How do I embed the jQuery?

    I just put it in < head > with < script > nesting.

    But it doesn’t seem to work.

    #114078
    Paulie_D
    Member

    My point was I know what you THINK you know what you want to do but without knowing what it is we can’t offer practical suggestions and it might be that there is a way to do it without selecting the li.

    It’s like you asking “How do I pick up this hammer?”

    I’m asking WHY you want a hammer in the first place. If it’s to drive in a screw then you are using the wrong tool.

    However, I’ll leave that there.

    Viewing 15 posts - 1 through 15 (of 18 total)
    • The forum ‘CSS’ is closed to new topics and replies.