Grow your CSS skills. Land your dream job.

select parent but not its children

  • # March 6, 2013 at 6:15 pm

    I have a list of items like this:

    Fight club
    1999
    Life of Pi
    2012

    and I want to put all the elements in an array and loop through them but just the items with the class `element` without their children.

    elements = $(“#content”).children(‘.element’);
    console.log(searchElements[0]’);

    but it gives the right length of array but its index contains the element with its children and I can’t `addClass` for example, gives me an error. It drives me crazy….

    # March 6, 2013 at 8:01 pm

    I don’t get it. With its children? I think it’s more the issue that `$(…)[0]` gives you a node object instead of a jQuery object. Try `$(…).eq(0)` instead.

    # March 6, 2013 at 8:58 pm

    @CrocoDillon check this simple [jsFiddle](http://jsfiddle.net/cparliaros/p4uzf/2/ “”) and the comments. I can select an element with a click function but I want also to be able to select elements using they keyboard keys. In this example I try to select just the first element pressing any key.

    note: in order for keydown function to work result window should be active by clicking anywhere in it. Also check the logs on the console.

    # March 6, 2013 at 9:55 pm

    `searchElements.eq(0).addClass(‘selected’);` or `$(searchElements[0]).addClass(‘selected’);`, I need sleep. If you need any more help I’ll be back tomorrow!

    # March 6, 2013 at 10:33 pm

    no worries, with your latest comment I understood what I was doing wrong. Time for me to sleep also. Thanks

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".