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.

2
  • 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() { Commented Dec 26, 2020 at 13:16
  • Maybe you want a regex filter function similar to this one stackoverflow.com/a/28734135/12684693 Commented Dec 26, 2020 at 13:18

2 Answers 2

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])")
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.