Skip to content Skip to sidebar Skip to footer

How To Find An Element With A Specific Attribute That Has A Specific Value

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

Solution 1:

You could use the attribute equals selector

$('[question-id="yourvalue"]').closest('div');

Solution 2:

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");

Post a Comment for "How To Find An Element With A Specific Attribute That Has A Specific Value"