Hi everyone,
I have done what I call a Twitter-like-signin-box (if you don't know what it looks like, go to www.twitter.com), at first it was pretty easy, I click on a button, the box (the form) opens and when I click on it again, the box closes.
But when I add the fact that the box have to close if it's open and I click everywhere else the box, I had to use the $('*') and I don't know if there is a better method.
Here my HTML (simplify) :
- <div id="signin_btn">
- <a href="/inscription.html">create_account</a>
- <a href="/login.html" id="signin_menu_btn">
- <span>connection_owner</span>
- </a>
- </div>
- <fieldset id="signin_menu">
- <form id="signinForm" action="/login.html" method="post">
- <div id="signinFields">
- <label for="signin_username">Username</label>
- <input type="text" name="signin[username]" id="signin_username">
- </div>
- </form>
- </fieldset>
And here is my JS (simplify too) :
- $(function()
- {
- $('*').click(function(e){
- e.stopPropagation();
- var $this = $(this);
- var $signin_menu_btn = $('#signin_menu_btn');
- var $signin_menu = $('#signin_menu');
- if ($this.parent().attr('id') == $signin_menu_btn.attr('id'))
- {
- $this = $(this).parent();
- e.preventDefault();
- if ($this.hasClass('open'))
- {
- $this.removeClass('open');
- $signin_menu.hide();
- } else {
- $this.addClass('open');
- $signin_menu
- .show()
- .position({
- of: $('#signin_btn'),
- my: 'right top',
- at: 'right bottom'
- })
- .css('top', parseInt($signin_menu.css('top'))+((ie7) ? 0 : 8));
- }
- } else if ($this.parents('#signin_menu').size() > 0) {
- // Nothing \o/
- } else if ($signin_menu_btn.hasClass('open')) {
- $signin_menu_btn.removeClass('open');
- $signin_menu.hide();
- }
- });
- });
It works really great (Chrome 5, Firefox 3.6, IE 7), but I'm a bit afraid of using $('*') (damn, I hate the '*' :( ), is it a problem here ?
PS : If you want to reuse my code, go for it ;)