jQuery $.closest() (Or whichever comes first)
Asked Answered
F

2

10

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?

Fractionate answered 31/8, 2009 at 0:50 Comment(0)
U
15

Have you tried:

$.closest("td, div")

?

Uremia answered 31/8, 2009 at 0:54 Comment(2)
So simple I should be ashamed ;) For some reason I failed to consider that $.closest() takes a regular selector like everything else.Fractionate
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.Uremia
M
0

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

$("#textareaId").parent() 
Marshamarshal answered 31/8, 2009 at 0:56 Comment(3)
I may not want the first parent in all cases.Fractionate
I don't disagree, but it suited your description.Marshamarshal
Note "textarea within a div" != "textarea immediately within a div" :) I could have been more clear though.Fractionate

© 2022 - 2024 — McMap. All rights reserved.