0

I have a simple script below where my buttons add a class to a single div element. I want to write a function that prints hi to the console whenever myClass moves, anyone have any tips?

 changeCurrent = function(id) {
    $("div").removeClass("myClass")
    $("#" + id + "").addClass("myClass");
  }

// I want to print hello every time we click a button 
// by detecting whenever the "current" class moves
$('document').on('change', '.section', function() {
  console.log("hello")
})
.myClass {
  color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onClick="changeCurrent('hi')">Hi</button>
<button onClick="changeCurrent('hello')">Hello</button>
<button onClick="changeCurrent('howdy')">Howdy</button>

<div class="section myClass" id="hi">Hi</div>
<div class="section" id="hello">Hello</div>
<div class="section" id="howdy">Howdy</div>
MayaGans
  • 1,815
  • 9
  • 30

0 Answers0