Forums

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

Home Forums JavaScript select parent but not its children

  • This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #43197
    cparliaros
    Member

    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….

    #127263
    CrocoDillon
    Participant

    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.

    #127267
    cparliaros
    Member

    @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.

    #127270
    CrocoDillon
    Participant

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

    #127273
    cparliaros
    Member

    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)
  • The forum ‘JavaScript’ is closed to new topics and replies.