1

On every click i tried to check if the class exists, and if the class found then load the enscroll plugin.
but the problem is, after find the class st2-wr || st2, its again load the enscroll plugin with click event because the class exists,

i want only once the plugin load after finding the class.

$('.embtn').delegate(this, 'click', function(){
    var $sec = $(this).closest('.sec'), 
        $parentSection = $sec.closest('.m_ms');

    if($parentSection.hasClass('st2-wr') || $parentSection.hasClass('st2')){
        $('.ms_box').enscroll({
                 showOnHover: true,
                 verticalTrackClass: 'track3',
                 verticalHandleClass: 'handle3'
         });
     }
});
2
  • which version of jquery are you using? Commented Jun 1, 2013 at 9:09
  • @roasted 1.9.1 version. Commented Jun 1, 2013 at 9:09

3 Answers 3

3

A possible snippet if i understood your goal:

$('.embtn').on('click', function () {
    var $sec = $(this).closest('.sec'),
        $parentSection = $sec.closest('.m_ms');

    if (!$('.ms_box').data('enscroll') && ($parentSection.hasClass('st2-wr') || $parentSection.hasClass('st2'))) {
        $('.ms_box').enscroll({
            showOnHover: true,
            verticalTrackClass: 'track3',
            verticalHandleClass: 'handle3'
        }).data('enscroll', true);

    }
});
Sign up to request clarification or add additional context in comments.

8 Comments

is there any specific reason behind using .on() instead of .delegate()??
no, and here using .on() my code is wrong. I updated as .on() delegation accept only 'selector'. .on() is just the new alias for bind/delegate & live, for consistency
sorry for wrong order of params using delegate, updated answer and i need coffee :)
:) ofcause, but one more thing, i didn't tried before the .data() method, but it's not working, i thing the possible reason is, i already used the enscroll() plugin on page load on other element, and this the second time that i want to use on the different element?? is this the reason of not working this code?
OOPPS a big mistake by me.. the code is working @rasted :), thankks
|
1

You can use one method

$("#foo").one("click", function() {
  alert("This will be displayed only once.");
});

Other way is to use some external boolean variable that will tell you if the plugin has been loaded.

3 Comments

one() method doesn't support delegation
@nKognito The handler is executed at most once per element.- one(), i am using the click event on a button, that have to press more then once..
@jogesh_pi Other way is to use some external boolean variable that will tell you if the plugin has been loaded.
0
(function(self) {
    function enscroll() {
        var $sec = $(this).closest('.sec'), 
            $parentSection = $sec.closest('.m_ms');

        if($parentSection.hasClass('st2-wr') || $parentSection.hasClass('st2')){
            $('.ms_box').enscroll({
                showOnHover: true,
                verticalTrackClass: 'track3',
                verticalHandleClass: 'handle3'
            });
            $('.embtn').off('click', self, enscroll);
        }
    }
    $('.embtn')
        .on('click', self, enscroll)
        .on('click', self, function() {
            // Other click handler executed each time
        });
})(this);

1 Comment

Maybe better because the click handler will be executed once and the dom tree browsed (closest) once too...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.