Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect event when class is added to li

Having html list as ,

<ul id="slider">
    <li class=""></li>
    <li class=""></li>
    <li class=""></li>
    <li class="selected"></li>
    <li class=""></li>
    <li class=""></li>
    <li class=""></li>
<ul>

I am adding selected class to any of the li element. Here i have added class="selected" to fourth li element.

Want to execute some code when class selected is added to any of the li element.

tried this one , but not working as expected.

<script>
    $(function() {
        $('#slider').bind('DOMSubtreeModified', function(e) {
            alert("Changed");
        });
    });
</script>
like image 379
Nishant Nawarkhede Avatar asked Jan 13 '14 10:01

Nishant Nawarkhede


1 Answers

Just invoke a function after you add the Class on the click event.

$(function() {

    function doSomething() {
      .....
    }

    $('ul').on('click', 'li' function(e) {
          $(this).addClass('selected');
          doSomething();
    });
});
like image 197
dcodesmith Avatar answered Sep 18 '22 13:09

dcodesmith