-5

I want to create a javascript array of all these following divs so I can write a for loop function over them. How should I create this array? Thanks

<div class="Org-popover-body-1">
1
</div>

<div class="Org-popover-body-2">
2
</div>

<div class="Org-popover-body-3">
3
</div>

<div class="Org-popover-body-4">
4
</div>

<div class="Org-popover-body-5">
5
</div>

<div class="Org-popover-body-6">
6
</div>
2

3 Answers 3

2

Simply use document.querySelectorAll() method and a wildcard such as [class^="Org-popover-"] which matches any class that begins with Org-popover-


console.log(document.querySelectorAll('[class^="Org-popover-"]'));
<div class="Org-popover-body-1">
1
</div>

<div class="Org-popover-body-2">
2
</div>

<div class="Org-popover-body-3">
3
</div>

<div class="Org-popover-body-4">
4
</div>

<div class="Org-popover-body-5">
5
</div>

<div class="Org-popover-body-6">
6
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

Two different ways you could go about this. You can use the starts with selector on the class to find them all.

With the markup you have

console.log( document.querySelectorAll('[class^="Org-popover-body-"]') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Org-popover-body-1">
1
</div>

<div class="Org-popover-body-2">
2
</div>

<div class="Org-popover-body-3">
3
</div>

<div class="Org-popover-body-4">
4
</div>

<div class="Org-popover-body-5">
5
</div>

<div class="Org-popover-body-6">
6
</div>

With a common class added.

You can give them all a common class and use that.

console.log( document.querySelectorAll('.Org-popover-body') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="Org-popover-body Org-popover-body-1">
    1
    </div>

    <div class="Org-popover-body Org-popover-body-2">
    2
    </div>

    <div class="Org-popover-body Org-popover-body-3">
    3
    </div>

    <div class="Org-popover-body Org-popover-body-4">
    4
    </div>

    <div class="Org-popover-body Org-popover-body-5">
    5
    </div>

    <div class="Org-popover-body Org-popover-body-6">
    6
    </div>

3 Comments

Tags: javascript arrays for-loop, no jQuery
@Alex refactored
Bootiful! Very bootiful!
1

Well would be easier if they had a common class, but since they do not, only option is attribute contains selector with querySelectorAll

var elems = document.querySelectorAll('[class*="Org-popover-body-"]')
console.log(elems)
<div class="Org-popover-body-1">
1
</div>

<div class="Org-popover-body-2">
2
</div>

<div class="Org-popover-body-3">
3
</div>

<div class="Org-popover-body-4">
4
</div>

<div class="Org-popover-body-5">
5
</div>

<div class="Org-popover-body-6">
6
</div>

3 Comments

Would I be correct in pointing out that [class*="Org-popover-body-"] will match with any class e.g. new-edited-Org-popover-body- which is less specific than using the caret (^) to define it as the start of the class name?
so swap to ^ not a big deal in this case. Just used * in case there were other classes the OP did not show since it can be anywhere in the attribute. Start with can fail too it they had other classes that followed same pattern.... Only safe bet is use a class and not have to match partials.
Yeah, you're right. I'm more concerned about the OP's possible restrictions.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.