Neil
Neil

Reputation: 5178

Find closest select box with a class from an element

From the starting point of an element that happens to be a select box (element has a class of: service_category_selection) I want to find another select box (element has a class of: service_selection).

I need to grab specifically the closest element with class: service_selection because I don't want to grab all of the elements with that class.

Snapshot of how far away that first select box is from the second select box:

find closest element

Assume that $(this) already contains the first select box. Now I just need to draw the route to the closest next select box with the class: service_selection.

I attempted to use .closest but it wasn't working for me.

Example: var el = $(this).closest(".service_selection");

Upvotes: 0

Views: 2370

Answers (2)

Pascal Boutin
Pascal Boutin

Reputation: 1264

Doing this kind of navigation is a bit hackish and easy to break since your logic highly rely on your layout. I suggest you to use a unique CSS class on that element and access it directly.

Upvotes: 3

tymeJV
tymeJV

Reputation: 104785

You need to get the overall top parent container - then find the element:

var el = $(this).closest(".row").find(".service_selection");

Upvotes: 3

Related Questions