Jump to content

jQuery for this parent's next sibling's 3rd child.


drayarms

Recommended Posts

Hello fellows,  I know this is an Ajax forum but I figured that Ajax experts must be gurus in Javascript/jQuery.  I'm a relative newbie at jQuery which I started learning as an alternative to JavaScript and all its annoying cross browser incompartibility issues.  Well I'm trying to create  selectors for a current element's distant relative for example,  I need a constructor for an element's parent's next sibling's second child.  Well here's what I came up with

 

$(this).parent().next().children().get(1)

 

I wonder if that makes any sense at all. I'm hoping it does. If not, who can show me how to construct this selector.  Also what if I wanted to make things a little bit murkier for example, what if I wanted a constructor for this element's parent's 3rd next sibling's second child?  Can someone please show me how to do that?

Link to comment
Share on other sites

Why access the element through such a complex relative path? Assign the target element an ID or class name you can use to keep things simple. That also has the benefit of not impacting your jQuery if you ever make small changes to your HTML structure - your selector is very susceptible to change.

Link to comment
Share on other sites

@adam, well this is one of those situations where using an id won't work. The elements in question are generated via a while loop and there will be several of them on the page, depending on user's input.  So appending unique id's won't work because there are going to be several identical yet unique elements.

Link to comment
Share on other sites

@adam, using classes won't work either because i want each element to target its own relative not all the corresponding relatives of its identical elements of which there will be plenty on the page.  But anyways I just learned from other forums that the selector I constructed above would work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.