Skip to content Skip to sidebar Skip to footer

Unable To Select Closest H2 Element Using Javascript

I would like to be able to select the closest h2 heading to the fieldset that contains the pizza selection options and set the text for the heading using jQuery. I have so far been

Solution 1:

closest() does only work to get the fieldset for your control. If you know that the h2 comes after the fieldset on the same level, you could do:

$(this).closest('fieldset').nextAll('h2').first();

But it depends on what you mean with 'closest'. You might want to have a look at the jQuery tree traversal functions.

Post a Comment for "Unable To Select Closest H2 Element Using Javascript"