Smart on-click event handler for parent element


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. // attach onclick event listener to all .item(s)
  2. $$('#content .toggle_item').each(function(item) {
  3. item.addEvent('click', function(event) {
  4. var target = $(event.target);
  5. // check that target is as expected
  6. if(target.hasClass('toggle')) {
  7. this.toggleClass('active');
  8. target.toggleClass('active');
  9. target.hasClass('active') ? target.innerHTML = 'verstecken…' : target.innerHTML = 'mehr…';
  10. }
  11. })
  12. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.