?
Solved

Jquey ui menu hiding

Posted on 2013-12-27
7
Medium Priority
?
745 Views
Last Modified: 2013-12-28
I have a set of 2 jquery buttons, one of them is logout and one will show a drop down menu, I have copied the example in the jquery ui page.
The code works pretty fine, so when I click on the settings button the menu is displayed, when I click on the the button or outside the menu is hiding when.
  What I am missing is when I press on the same button (settings) the menu hides if it is displayed, can some one tell me how to do it please? below is my html code:
<?php

?>
<html>
    <head>
        <link rel="stylesheet" href="css/jquery.ui.all.css">
        <link rel="stylesheet" href="css/ribbon.css">
        <link rel="stylesheet" href="css/demos.css">
        <style>
           #toolbar {
                        padding: 4px;
                        background: #FFFFFF;
                        display: inline-block;
                }
                /* support: IE7 */
            *+html #toolbar {
                        display: inline;
                }
            .ui-menu {
                      width: 150px;
                      background: #FFFFFF;
                     }
        </style>
        <meta charset="utf-8">
                <script src="js/jquery-1.10.1.min.js"></script>
                <script src="js/jquery-ui-1.10.3.custom.min.js"></script>
                <script>
                 $(function() {
                  $( "#logout" ).button({
                   text: false,
                   icons: {
                     primary: "ui-icon-key"
                     }
                    });
                   $( "#settings" ).button({
                   text: false,
                   icons: {
                     primary: "ui-icon-gear",
                     secondary: "ui-icon-triangle-1-s"
                     }
                     })
                     .click(function() {
                      var menu = $( this ).parent().next().show().position({
                      my: "left top",
                      at: "left bottom",
                      of: this
                      });
                      $( document ).on( "click", function() {
                      menu.hide();
                      });
                      return false;
                      })
                      .parent()
                        .buttonset()
                          .next()
                           .hide()
                           .menu();
                      $("menu").mouseout( function(){
                        $("menu").hide();
                      });
                 });
                </script>



 </head>
 <body bgcolor="white">
<div id="header">
<div class="ribbon"><div class="ribbon-stitches-top"></div>
<strong class="ribbon-content">Welcome</strong>
<div class="ribbon-stitches-bottom"></div></div>
<div id="menu"  style="text-align: right">
<div id="toolbar" class="ui-widget-header ui-corner-all">
<button id="logout">Logout</button>
<button id="settings">Settings</button>
</div>
  <ul id="menu" >
    <li><a href="#">Open...</a></li>
    <li><a href="#">Save</a></li>
    <li><a href="#">Delete</a></li>
  </ul>
</div>
</div>
 </body>
</html>

Open in new window

0
Comment
Question by:Ashraf Hassanein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 

Author Comment

by:Ashraf Hassanein
ID: 39743262
Hi Scott sorry for that, I am new to EE, but will do that in my next questions
0
 
LVL 20

Expert Comment

by:Mark Brady
ID: 39743532
You should use jquery's .toggle() method. instead of doing a show() or hide() on the menu element change that so that the onClick handler uses toggle(). That way you can get rid of some code as well. Small example
.click: function() {
    $('menu_element').toggle();
}

Open in new window

0
 
LVL 82

Accepted Solution

by:
leakim971 earned 2000 total points
ID: 39743576
Test page : http://jsfiddle.net/ybLFm/

$(function() {

    var $logout = $("#logout").button({ text: false, icons: { primary: "ui-icon-key" } });
	var $settings = $("#settings").button({ text: false, icons: { primary: "ui-icon-gear", secondary: "ui-icon-triangle-1-s" } });
    var $menu = $("#settings").parent().next().show();

    $(document).click(function() {
        $menu.hide();
        $settings.removeClass("active");
    });

    $settings.click(function(evt) {
        evt.stopImmediatePropagation();
        $(this).toggleClass("active")
        $menu.css("display", $(this).hasClass("active")?"block":"none");
        $menu.position({ my: "left top", at: "left bottom", of: this });
	});

    $settings.parent().buttonset().next().hide().menu();

    $menu.hover(function() {
        $(this).addClass("overme");
    }, function() {
        if($(this).hasClass("overme")) {        
    		$(this).hide();
            $(this).removeClass("overme");
            $settings.removeClass("active");
        }
    });
    
    $menu.click(function(evt) {
        alert($(evt.target).text());
    });
});

Open in new window

0
 

Author Closing Comment

by:Ashraf Hassanein
ID: 39743724
Great very clear support
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question