3

I have the following HTML:

<div class="questionItem">
<input data-val="true" data-val-number="The field MappingId must be a number."
data-val-required="The MappingId field is required."
id="answers_2__MappingId" name="answers[2].MappingId" type="hidden" value="23" />
<input id="answers_2__Item" name="answers[2].Item" type="hidden" value="B0300" />
<input id="answers_2__SetAnswer" name="answers[2].SetAnswer" type="hidden" value="^" />
</div>

jQuery will get me all the questionItems with this:

var coll = $('.questionItem');

I don't need all of them, I simply need the questionItems that have a child input with a name of answers[/d+].SetAnswer and value of ?.

How does one construct that has() clause?

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
Sam Carleton
  • 1,339
  • 7
  • 23
  • 45
  • This may help: http://stackoverflow.com/questions/3086554/find-all-elements-based-on-ids-using-regex-on-jquery-selector – nhahtdh Jun 08 '12 at 16:04

2 Answers2

2

It should be something like this:

var coll = $(".questionItem").filter(function() {
    return $(this).find(":input").filter(function() {
        return /^answers\[\d+\]\.SetAnswer$/.test(this.name) && this.value == "?";
    }).length > 0;
});

DEMO: http://jsfiddle.net/nUNJd/

VisioN
  • 143,310
  • 32
  • 282
  • 281
1

Regular expressions aren't needed here. Just use :has and attribute selectors:

$(".questionItem:has(input[name$='.SetAnswer'][value='?'])");

Fiddle: http://jsfiddle.net/jonathansampson/S8kft/

Sampson
  • 265,109
  • 74
  • 539
  • 565