Search code examples
jqueryjquery-selectorssizzle

jQuery/Sizzle selector to find current element's parent?


Is there a way I can get the selected element's parent using only jQuery's/sizzle's CSS selectors?

I need to be able to get an element's parent while using jQuery, but I'm unable use jQuery('#myelement').parent() since I'm receiving the selector as a string and the "user" needs to be able to move back up the tree.

I can't see anything in the docs, so I'm wondering whether it's undocumented or if there's a hack I can use?


Solution

  • Since CSS can't traverse back up the tree like XPath can, I'm doing a bit of munging and checking for the character < in the string, splitting on that and calling .parent() manually. Its not optimal but it's working.