Forums

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

Home Forums JavaScript Jquery .parent().parent()… easier way to target a previous element?

  • This topic is empty.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #32579
    paperrobots
    Participant

    Is there an easier way to select a previous element with jquery rather then chaining .parent().parent().parent()? I have multiple list items that each need individual functionality. So I’m trying to target elements within each one based on the button clicked. What would be the easiest way to target other elements?










  • #47883

    You can use the parents() selector as opposed to the parent() selector to travel up the dom multiple levels. In this case, are you just looking for the list item? Maybe something like:

    $(this).parents('li').prev();
    #47634

    @Mottie – good call there. Always better safe then sorry.

    #47609
    paperrobots
    Participant

    Nice I wasn’t aware of .closest! It didn’t work with the .prev() though. So I took it off and it seems to be working.

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