views:

241

answers:

2

While working a project tonight, I ended up using one .js resource file for two different pages. One page contains a textarea within a div, and another contains a textarea within a td. Wanting to work with the siblings of this textarea, and other children of its parent, I wondered how you could best implement "closest div or td, whichever comes first" logic via jQuery syntax.

What are your guys' suggestions?

+4  A: 

Have you tried:

$.closest("td, div")

?

cletus
So simple I should be ashamed ;) For some reason I failed to consider that $.closest() takes a regular selector like everything else.
Jonathan Sampson
Did it work? I wasn't sure it would. If it does, it'd be good to know cos I could see it being useful.
cletus
Yes, it worked Cletus :)
Jonathan Sampson
A: 

As an alternative to what cletus mentioned, what is wrong with simply:

$("#textareaId").parent()
Marc
I may not want the first parent in all cases.
Jonathan Sampson
I don't disagree, but it suited your description.
Marc
Note "textarea within a div" != "textarea immediately within a div" :) I could have been more clear though.
Jonathan Sampson