0

as title says my script don't want to work when I click outside the drop menu of button Sign in

jsfiddle Demo

$(document).ready(function() {
  $(".openmenu").click(function() {
    var X = $(this).attr('id');
    if (X == 1) {
      $(".submenu").hide();
      $(this).attr('id', '0');
    } else {
      $(".submenu").show();
      $(this).attr('id', '1');
    }
  });
  $(".submenu").mouseup(function() {
    return false
  });
  $(".openmenu").mouseup(function() {
    return false
  });
  $(document).mouseup(function() {
    $(".submenu").hide();
    $(".openmenu").attr('id', '');
  });
});

$(document).ready(function() {
  myInit()
})


$(document).ready(function() {
  myInit()
})

function myInit() {
  $('.openmenu').click(function() {
    $('.openmenu').removeClass('active')
    $(this).addClass('active')
  })
}
/* RESET CSS */

html,
body,
div,
span,
applet,
object,
iframe,
h1,
h2,
h3,
h4,
h5,
h6,
p,
blockquote,
pre,
a,
abbr,
acronym,
address,
big,
cite,
code,
del,
dfn,
em,
img,
ins,
kbd,
q,
s,
samp,
small,
strike,
strong,
sub,
sup,
tt,
var,
b,
u,
i,
center,
dl,
dt,
dd,
ol,
ul,
li,
fieldset,
form,
label,
legend,
table,
caption,
tbody,
tfoot,
thead,
tr,
th,
td,
article,
aside,
canvas,
details,
embed,
figure,
figcaption,
footer,
header,
hgroup,
menu,
nav,
output,
ruby,
section,
summary,
time,
mark,
audio,
video {
  margin: 0;
  padding: 0;
  border: 0;
  font-size: 100%;
  font: inherit;
  vertical-align: baseline;
}
/* HTML5 display-role reset for older browsers */

article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
menu,
nav,
section {
  display: block;
}
ol,
ul {
  list-style: none;
}
blockquote,
q {
  quotes: none;
}
blockquote:before,
blockquote:after,
q:before,
q:after {
  content: '';
  content: none;
}
table {
  border-collapse: collapse;
  border-spacing: 0;
}
/* BODY */

body {
  font-family: "Myraid Pro", Arial;
  overflow-y: scroll;
  overflow-x: hidden;
  color: #333;
  font-size: 14px;
}
.clear {
  clear: both;
}
/* FLOATS */

.left {
  float: left;
}
.right {
  float: right;
}
#top-bar {
  background: #619B27;
}
#top-bar-inner {
  margin: auto;
  width: 500px;
  font-size: 14px;
}
#top-bar {
  background: #619B27;
}
#top-bar-inner {
  margin: auto;
  width: 300px;
  font-size: 14px;
  position: relative;
}
#top-bar li {
  display: inline-block;
  margin-left: 10px;
}
#top-bar a {
  display: block;
  padding: 10px;
  color: #FFF;
  text-decoration: none;
  outline: 0;
}
#top-bar a:hover {
  background: #4d7c1f;
}
.active {
  background: #CC0;
}
.submenu {
  background: #CC0;
  position: absolute;
  right: 0;
  width: 200px;
  height: 250px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="top-bar">
  <div id="top-bar-inner">
    <div class="right">
      <ul>
        <li><a href="/">Register</a>
        </li>
        <li><a href="#" class="openmenu">Sign In</a>
        </li>
        <div class="submenu" style="display: none;">
          <p>test</p>
        </div>
      </ul>
    </div>
    <div class="clear"></div>
  </div>
</div>

When I click on Sign in button it changes to active class which change the background of the button and that's fine but it don't want to work when i click outside, i want the active class to be removed and the css colors return. What i'm doing wrong?

1 Answer 1

2

Where your mouseup event is just add:

.removeClass("active");

to

$(".openmenu").attr('id', '');

like this:

$(document).mouseup(function(){
   $(".submenu").hide();
   $(".openmenu").attr('id', '').removeClass("active"); <-----
});

FIDDLE

And in the future, please don't copy the jquery library into the fiddle, just choose jquery from the side menu

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

1 Comment

@JohnSmith you're welcome. Happy New Year to you 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.