Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get dk.open() to work with onlick event #271

Closed
pierrefaure opened this issue Feb 19, 2015 · 6 comments
Closed

Can't get dk.open() to work with onlick event #271

pierrefaure opened this issue Feb 19, 2015 · 6 comments

Comments

@pierrefaure
Copy link
Contributor

Hi guys, I spent a few hours on the open() function and I can't find why it doesn't want to open.

Basically I want to open a dropkick element on click.

That works:

dk = new Dropkick("#my-select"); dk.open();

But that open the dropkick element when the page load.

  • I tried the JS way:
    document.getElementById("my-submit-btn").onclick = function(){ dk.open() };
  • the jquery way:
    $('#my-select').on('click', '#my-submit-btn', function(e) { e.preventDefault(); dk.open(); });

In the two last cases, if I console.log(dk.open());, I get undefined and when I console.log(dk.open()); without any onclick event I get a boolean value.

The funny thing is that if I replace dk.open() by dk.disable() everything is working perfectly ...

Am I missing something?

Any help is appreciated :)
Thanks!

@wwilsman
Copy link
Collaborator

You might have trouble with opening it while clicking another element because it closes immediately after due to the document event listener.

I'll do some testing to see if I can work a solution into DK itself.

Meanwhile, try wrapping it in a short timeout to wait until after the document click fires. Something like 10 ms might be enough time.

@Robdel12
Copy link
Owner

Yep, here's a bin proving it: http://jsbin.com/qugoxevima/2/edit?html,js,output

Marking as critical because the open API as of right now is broken.

@pierrefaure
Copy link
Contributor Author

Thanks @wwilsman that works like a charm! :)

If someone get on this issue, here is the code I use:

dk = new Dropkick("#sortby-select", {mobile: true});

jQuery(document).ready(function($) {

  $('#my-container').on('click', '#my-btn', function(e)
     setTimeout(function(){
      dk.open();
    }, 5);
  });

});

@Robdel12
Copy link
Owner

@pierrefaure you should be able to use the latest master. I haven't created a release yet because I'm not sure this is the permeant fix or not.

wwilsman added a commit that referenced this issue Feb 20, 2015
@Robdel12
Copy link
Owner

Fixed with DropKick 2.1.2! Thank you @pierrefaure for the issue, we appreciate it :)

@pierrefaure
Copy link
Contributor Author

You're very welcome, thanks for the quick fix :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants