7

I want to use the javascript function getElementsByClassName to loop in some html divs that don't have the exactly class name, but it starts with the same text: "akordeon-item"

So, the html text looks like this:

<div class="akordeon-item akordeon-item-first">

<div>
    <span class="userid">
          blabla@yahoo.com
    </span>
</div>
</div>
<div class="akordeon-item akordeon-item-second">

<div>
    <span class="userid">
          john_doe@yahoo.com
    </span>
</div>
</div>
<div class="akordeon-item akordeon-item-third">

<div>
    <span class="userid">
          john_doe2@yahoo.com
    </span>
 </div>
 </div>

 <div class="akordeon-item akordeon-item-fourth">

<div>
    <span class="userid">
          john_doe3@yahoo.com
    </span>
</div>

Also, the javascript code I tried is this one:

var slides = getElementsByClassName(".akordeon-item");
for(var i = 0; i < slides.length; i++)
{
   alert("element : "+slides[i]);
}

I also tried searching for this string inside the getElementsByClassName method : "akordeon-item"

How can I make this loop work? Please help...

user3256429
  • 146
  • 1
  • 1
  • 12

2 Answers2

25

There is a bunch of special selectors you can make use of:

^= is starts with

$= is ends with    
=  is exactly equal
!= is not equal
*= is contains

Try:

$.each($('[class^="akordeon-item"]'), function(key, value) {
    console.log(key, value);
});

Alternative ways to iterate over the set:

$('[class^="akordeon-item"]').each(function(key, value) {
    console.log(key, value);
});

Or using a for loop:

var slides = $('[class^="akordeon-item"]');
for (var i = 0; i < slides.length; i++) {
    console.log(slides[i]);
}
martynas
  • 12,120
  • 3
  • 55
  • 60
2
$('div[class^="akordeon-item"]').each(function(index){});
RGS
  • 5,131
  • 6
  • 37
  • 65