0

I would like to change the color of some badges in an list.

<div id="tree">
<ul class="list-group">
    <li class="list-group-item node-tree" ><span class="icon expand-icon glyphicon glyphicon-minus"></span><span class="icon node-icon"></span><a href="#" style="color:inherit;">Test</a><span class="badge">123</span><span class="badge">123</span><span class="badge">3223</span><span class="badge">23</span><span class="badge">323</span></li>
    <li class="list-group-item node-tree"><span class="icon expand-icon glyphicon glyphicon-minus"></span><span class="icon node-icon"></span><a href="#" style="color:inherit;">Test 1</a><span class="badge">321</span><span class="badge">123</span><span class="badge">3223</span><span class="badge">23</span><span class="badge">323</span></li>
</ul>
</div>

It still works with jQuery, for example with:

 $("#tree .list-group-item").each(function(){

            $(this).find('.badge:eq(4)').css('background-color','#bf5329');
            $(this).find('.badge:eq(4)').css('color','#fff');

            $(this).find('.badge:eq(3)').css('background-color','#2ab27b');
            $(this).find('.badge:eq(3)').css('color','#fff');

            $(this).find('.badge:eq(2)').css('background-color','#bf5329');
            $(this).find('.badge:eq(2)').css('color','#fff');

            $(this).find('.badge:eq(1)').css('background-color','#2ab27b');
            $(this).find('.badge:eq(1)').css('color','#fff');

            $(this).find('.badge:eq(0)').css('background-color','#3B4752');
            $(this).find('.badge:eq(0)').css('color','#fff');
        });

But my Problem is, when ill change the tree, i need to call the method again (and again..) - so i would prefer to create the same with CSS only, but when ill try with:

#tree .list-group-item .badge:nth-child(1) {
    background-color: #bf5329;   
}

Its not setting EVERY .list-group-item, only the first one (and there are about 100 "rows".

Any ideas?

Here is a fiddle: https://jsfiddle.net/DTcHh/30190/

Update with CSS: https://jsfiddle.net/DTcHh/30192/

Thanks for your answers - but ill still have a problem if there are some more

 <span>

inside my

 .list-group-item

In this case its a treeview, and there are some "indent" classes. Check out here:

https://jsfiddle.net/DTcHh/30196/

Any more ideas?

derdida
  • 14,784
  • 16
  • 90
  • 139

2 Answers2

1

The equivalent of $('.badge').eq(0) in CSS here will be .badge:nth-child(4) since the first .badge is the 4th child of it's parent. Then you can increment that by 1 to target the rest of them.

/* Latest compiled and minified CSS included as External Resource*/

/* Optional theme */
@import url('//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css');

body {
    margin: 10px;
}

span.badge {
  color: #fff;
}
span.badge:nth-child(4) {
  background: #3B4752;
}
span.badge:nth-child(5) {
  background: #2ab27b;
}
span.badge:nth-child(6) {
  background: #bf5329;
}
span.badge:nth-child(7) {
  background: #2ab27b;
}
span.badge:nth-child(8) {
  background: #bf5329;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet"/>
<div id="tree">
<ul class="list-group">
<li class="list-group-item node-tree" data-nodeid="0" style="color:undefined;background-color:undefined;"><span class="icon expand-icon glyphicon glyphicon-minus"></span><span class="icon node-icon"></span><a href="#" style="color:inherit;">Test</a><span class="badge">123</span><span class="badge">123</span><span class="badge">3223</span><span class="badge">23</span><span class="badge">323</span></li>
<li class="list-group-item node-tree" data-nodeid="0" style="color:undefined;background-color:undefined;"><span class="icon expand-icon glyphicon glyphicon-minus"></span><span class="icon node-icon"></span><a href="#" style="color:inherit;">Test 1</a><span class="badge">321</span><span class="badge">123</span><span class="badge">3223</span><span class="badge">23</span><span class="badge">323</span></li>
</ul>
</div>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
  • Thanks for your answer - but can u please explain why u use 4,5,6,7,8 element? – derdida Feb 24 '17 at 21:32
  • 1
    @derdida `nth-child(1)` doesn't target the first instance of an element, it targets the selector if it's the first child of an element. – Michael Coker Feb 24 '17 at 21:34
  • Ah thanks for your answer! Gimme one second please ill try it out with my list of about 100 rows. – derdida Feb 24 '17 at 21:34
  • So my problem now is when ill use and additional class (in this case "indent, to make an tree" - i am unable to use css in this case (because there are additional inside my
  • - is it possible to solve this with CSS only? Check out here: https://jsfiddle.net/DTcHh/30195/
  • – derdida Feb 24 '17 at 21:39
  • @derdida give the little icons you want to color a consistent parent so that `:nth-child()` will be the same between the rows. https://jsfiddle.net/DTcHh/30197/ – Michael Coker Feb 24 '17 at 21:45
  • I just want to color the existing badges - this is an existing treeview plugin (otherwise i need to extend that in the plugin) - can i explain please what have you changed in the html? i cant find it ;) - and thanks for your help! – derdida Feb 24 '17 at 21:49
  • 1
    @derdida Just wrap the badges in `...` then make the CSS change I made which references different indexes of `:nth-child()` – Michael Coker Feb 24 '17 at 21:50
  • 1
    Thanks again - i will extend the plugin with this tiny wrapper! – derdida Feb 24 '17 at 21:55