drayarms Posted August 30, 2011 Share Posted August 30, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/ Share on other sites More sharing options...
Adam Posted August 30, 2011 Share Posted August 30, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1263506 Share on other sites More sharing options...
drayarms Posted August 30, 2011 Author Share Posted August 30, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1263656 Share on other sites More sharing options...
Adam Posted August 30, 2011 Share Posted August 30, 2011 You can still assign them a shared class name and access the n'th child. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1263669 Share on other sites More sharing options...
drayarms Posted August 31, 2011 Author Share Posted August 31, 2011 @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. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1263824 Share on other sites More sharing options...
Adam Posted August 31, 2011 Share Posted August 31, 2011 I never said it wouldn't work, I said it was very susceptible to change. Which it is. If you provide the mark-up you're working with I'll show you what I mean. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1263876 Share on other sites More sharing options...
drayarms Posted August 31, 2011 Author Share Posted August 31, 2011 @Adam, I'm not sure what you mean by susceptibe to change. Do you mean that some of the methods are becoming depricated? Anywyas, I'm still working on the markup and I promise I'll show you as soon as I'm done. Quote Link to comment https://forums.phpfreaks.com/topic/246010-jquery-for-this-parents-next-siblings-3rd-child/#findComment-1264039 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.