4

I have found how to get the elements whose id starts with aName and ends with EditMode as below: jQuery selector for matching at start AND end of ID?

$('[id ^=aName][id $=EditMode]')

I have also found how to get the elements of type input and select as below: jQuery find input type (but also for select)

$('input, select');

How can I combine both these to get all the input and select elements whose ids start with aName and end with EditMode

Reema
  • 587
  • 3
  • 12
  • 37

2 Answers2

4

Use .filter()

Reduce the set of matched elements to those that match the selector or pass the function's test.

$('input, select').filter('[id^=aName][id$=EditMode]')....

OR, Combine selectors

$('input[id^=aName][id$=EditMode], select[id^=aName][id$=EditMode]').....
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 1
    Using filter is probably the best way :) also good to note: tagname selectors are more efficient than attribute selectors, so always select by tagname first, and then filter using attributes: see https://stackoverflow.com/questions/6533285/jquery-select-by-class-vs-select-by-attribute – Terry Aug 22 '17 at 07:28
1

You can use jquery filter() method:

$('input,select').filter('[id ^=aName][id $=EditMode]')

See demo below:

console.log($('input,select').filter('[id ^=aName][id $=EditMode]').get());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input type="text" id="aName_1_EditMode"/>
<input type="text" id="aName_2_EditMode"/>
<input type="text" id="aName_3_EditMode"/>
<input type="text" id="aName_3_"/>
<input type="text" id="3_EditMode"/>

<select type="text" id="aName_4_EditMode">
  <option value="1">1</option>
  <option value="2">2</option>
</select>
kukkuz
  • 41,512
  • 6
  • 59
  • 95