Search code examples
jqueryaem

How to get third occurence of a class in jquery and add another class to it


I have an requirement to get the third occurence of a class name

The class name is coral-buttongroup.rte-toolbar. How can i get the third occurence of this class and then append something to this.

I have tried with

$('.coral-buttongroup.rte-toolbar').eq(2).append('<button  class="desc-count"></button>');

But this isn't working.


Solution

  • Your code works

    • You had nested double quotes, but you fixed that
    • You need to load jQuery
    • Your code has to wait until the elements exist

    $(function() {
      $('.coral-buttongroup.rte-toolbar').eq(2)
        .append('<button  class="desc-count">Click</button>');
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class="coral-buttongroup rte-toolbar">1</div>
    <div class="coral-buttongroup rte-toolbar">2</div>
    <div class="coral-buttongroup rte-toolbar">3</div>
    <div class="coral-buttongroup rte-toolbar">4</div>