0

I need to select the list of select boxes with the id pattern as follows.

id_social_media-0
id_social_media-1
id_social_media-2

I can not use $('select[id^=id_social_media-').each() since there is a hidden element as id_social_media-__empty. I tried with id_social_media-\d, but it does not work.

Indika Rajapaksha
  • 1,056
  • 1
  • 14
  • 36
  • is that hidden element also select-box ? Also has that element is hidden you can use `:visible` i.e :`$('select[id^=id_social_media-]:visible').each(function() {` – Swati Dec 26 '20 at 13:16
  • Maybe you want a regex filter function similar to this one https://stackoverflow.com/a/28734135/12684693 – FSDford Dec 26 '20 at 13:18

2 Answers2

1

You could use the :not selector to exclude results you don't want:

$("select[id^=id_social_media-]:not([id=id_social_media-__empty])")
PHP Guru
  • 1,301
  • 11
  • 20
0

Why not simple do an oldschool for loop?

for(let index = 0; index < numberOfItems; index++)
{
   let Element = $('select[id^=id_social_media-'+index);
   ...

If you don’t know the number, use a while loop and stop on no result in element:

var index = 0,
    elements = Array(),
    Element = $('select[id^=id_social_media-'+index);
while (Element.length) {
    elements[index++] = Element;
    Element = $('select[id^=id_social_media-'+index);
}
PHP Guru
  • 1,301
  • 11
  • 20
Thallius
  • 2,482
  • 2
  • 18
  • 36