0

How do a find an element which has the attribute "question-id" and equals a specific value? From there, i need to find the closest div (the wrapper of this element).

Thanks

Kenci
  • 4,794
  • 15
  • 64
  • 108

2 Answers2

3

You could use the attribute equals selector

$('[question-id="yourvalue"]').closest('div');
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192
0

Use an Attribute Equals selector and the closest() method:

var wrapper = $("[question-id='yourValue']").closest("div");

As an aside, note that you can use an HTML5 data attribute (e.g. data-question-id) to embed information in your element without putting the validity of your markup in jeopardy. In that case, the code above would become:

var wrapper = $("[data-question-id='yourValue']").closest("div");
Frédéric Hamidi
  • 258,201
  • 41
  • 486
  • 479