1

I want to initiate a plugin only if clicked on its parent element (because that element is being appended with jQuery so it does not exist when page loads), So I am trying following code:

$(document).on('click', '.wrap', function(){
    ColorPicker(

        document.getElementById('slider'),
        document.getElementById('picker'),

        function(hex, hsv, rgb) {

        });
});

This works fine, but every time I click on the .wrap, it duplicates (please check the demo to see the problem). Is there anyway to fix it?

Demo: http://jsfiddle.net/rhzzG/

(Click in the box to see the problem)

Thanks.

2 Answers 2

2

Rather than using on() to trigger this event every time the element is clicked, simply use jQuery's one() method to only fire it once:

$(document).one('click', '.wrap', function(){ ... });

ColorPicker will then take over from there with handling its own events.

JSFiddle demo.

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

Comments

2

You may use one instead

$(document).one('click', '.wrap', function(){
    //...
});

Comments

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.