jQuery addClass example

Change class name on click in jQuery

by Dmitri Ganenco

HTML

<BODY>
  <DIV ID="test"></DIV>
  <button id="create">create it!</button>
</BODY>

JavaScript

$(document).ready(function() {
  $('#create').on('click', function() {
    $('#test').append("<table class='dynamic'><tr><td><div class='focus'>Name</div></td></tr><tr><td>address</td></tr></table>")
  });

  $(document).on({
  'keydown': function(e) {
    var $table = $(document).find('table');
    if ($table.length && $table.hasClass('dynamic')) {
      alert('Table found!');
    }
  },
  'mouseenter': function(e){
  var $target = $(e.target);
  var $parentTable = $target.closest('table');
  	if($parentTable.length && $parentTable.hasClass('dynamic') && $target.hasClass('focus')){
    alert('Ok');
    }
  });
  }
});