javascript - Using on('click') instead of click -
after being suggested make more targeted question, have issue.
currently have code
omshortcodes.modules.toggle = { init: function() { jquery('.omsc-accordion .omsc-toggle').addclass('omsc-in-accordion').find('.omsc-toggle-title').click(function(){ var $toggle=jquery(this).parent(); if($toggle.hasclass('omsc-expanded')) { $toggle.removeclass('omsc-expanded'); $toggle.find('.omsc-toggle-inner').slideup(300); return false; } var $acc=$toggle.parents('.omsc-accordion'); if(!$acc.hasclass('omsc-multiopen')) $acc.find('.omsc-toggle.omsc-expanded').removeclass('omsc-expanded').find('.omsc-toggle-inner').slideup(300); $toggle.find('.omsc-toggle-inner').slidedown(300,function(){ var h=jquery(this).parent().height(); var pos=jquery(this).parent().find('.omsc-toggle-title').offset(); var scroll=jquery(window).scrolltop(); var wh=jquery(window).height(); if(pos.top < scroll || (pos.top > scroll && pos.top+h > scroll+wh)) jquery('html,body').animate({ scrolltop: pos.top+'px' }, 200); }).find('iframe[src*="maps.google"]').each(function(){ jquery(this).attr('src',jquery(this).attr('src')); }); $toggle.addclass('omsc-expanded'); }); jquery('.omsc-toggle').not('.in-accordion').find('.omsc-toggle-title').click(function(){ var $toggle=jquery(this).parent(); var $inner=$toggle.find('.omsc-toggle-inner'); if(!$inner.length) return false; if($inner.is(':animated')) return false; $toggle.toggleclass('omsc-expanded'); $inner.slidetoggle(300); if($toggle.hasclass('omsc-expanded')) { $inner.find('iframe[src*="maps.google"]').each(function(){ jquery(this).attr('src',jquery(this).attr('src')); }); } return false; }); } }
but doesn't work ajax, when page loaded directly.
i've been advised use on('click') instead of click.
i have tried
jquery('.omsc-accordion .omsc-toggle').addclass('omsc-in-accordion').find('.omsc-toggle-title').on('click', '.omsc-accordion', function(){ jquery('.omsc-accordion .omsc-toggle').addclass('omsc-in-accordion').find('.omsc-toggle-title').on('click', 'omsc-accordion', function(){ jquery('.omsc-accordion .omsc-toggle').addclass('omsc-in-accordion').find('.omsc-toggle-title').on('click', '.omsc-accordion .omsc-toggle', function(){
but 3 make nothing @ all. implementing incorrectly?
jquery('.omsc-toggle').not('.in-accordion').find('.omsc-toggle-title').click
attach click handler events exist when page loads. attach handler future elements, use delegation features of on
.
jquery('#container').on('click', '.omsc-toggle:not(.in-accordion) .omsc-toggle-title', function() { console.log('clicked!'); });
where #container
element contains of .omsc-toggle
elements. not recommended attach event handlers $(document)
.
Comments
Post a Comment