add/remove active class on parent element when clicked - javascript

I'm having some issues with my javascript - trying to get this to work.
To begin with, here is what I am wanting this to do:
1.) Add/Remove 'active' class to parent element on click
2.) When on child element, any clicking inside child element makes it close - Need help to make it NOT do that...if any clicking is going on inside the child element, it needs to stay open and not close up...
3.) Besides clicking on the parent element to open/close the child element, is there a way to allow clicking anywhere on the page to close the child element rather than only clicking on the parent element to close the child element up?
JS:
$(document).ready(function () {
$("#logsterIn_profile").click(function () {
$(".logsterIn_profile").slideToggle('200');
$("#logsterIn_profile").addClass('active');
});
});
To see what i currently have in action, FIDDLE ME DEMO
Any help would be much appreciated!

Here's the answer to all three points:
http://jsfiddle.net/vnny7/
$(document).ready(function () {
$("html").click(function () {
$(".logsterIn_profile").slideToggle('200');
$("#logsterIn_profile").toggleClass('active');
});
$(".logsterIn_profile").click(function( event ) {
event.stopPropagation();
});
});
use toggleClass to go between "active" and ""
use stopPropagation on the child element to put the click event on it, and stop it from "propagating" the event up to its parent
to get a click anywhere on the page to open/close the child, just have the click event tied to "html"

Do you want the div to open AND close when the user clicks outside the parent div, or just close?
By your description, you sound like you only want the div to close, but not open if the user clicks outside. If that's the case, you need to check if the child is open or closed when the user clicks outside of the parent, as follows:
$(document).ready(function () {
$("html").click(function(e) {
if (e.target == document.getElementById("logsterIn_profile")){
$(".logsterIn_profile").slideToggle('200');
$("#logsterIn_profile").toggleClass('active');
}else{
if($("#logsterIn_profile").hasClass('active')){
$(".logsterIn_profile").slideToggle('200');
$("#logsterIn_profile").toggleClass('active');
}
}
});
$(".logsterIn_profile").click(function( event ) {
event.stopPropagation();
});
});
You can check it out at this fiddle

http://jsfiddle.net/AzSXL/8/
$(document).ready(function () {
$("#logsterIn_profile").click(function () {
$("#logsterIn_profile").toggleClass('active');
$(".logsterIn_profile").slideToggle('200');
});
$(".logsterIn_profile").bind('click',function(e) {
e.stopPropagation();
});
$("html").click(function () {
$("#logsterIn_profile").toggleClass('active');
$(".logsterIn_profile").slideToggle('200');
}
});

Related

Removed class when click outside div but keep removed when div is closed

I have a div which open when I click on the question mark icon (upper right of the document).
When clicked I add a class which transform the question mark icon in close icon.
When I click outside the div, the div closed and the class is removed, closed icon becomes the question mark icon.
But now when I click in the document, it add and removed the class whenever the div is open or closed.
If anybody have an idea how I can removed the class when cliked on the document only when the div is open.
Thanks
jQuery('.showmenu').click(function(e){
e.stopPropagation();
jQuery('.about-btn').toggleClass( "active");
jQuery('.about').slideToggle('fast');
});
jQuery('.about').click(function(e){
e.stopPropagation();
});
jQuery(document).click(function(){
jQuery('.about-btn').toggleClass( "active");
jQuery('.about').slideUp('fast');
});
IMO
add/bind $(document).click only when/on $('.showmenu').click function.
Later do $(document).unbind('click') in $(document).click function
ex:
$('.showmenu').click(function (e) {
e.stopPropagation();
$('.about-btn').toggleClass("active");
$('.about').slideToggle('fast');
//Binding click function on document
$(document).click(function () {
$('.about-btn').toggleClass("active");
$('.about').slideUp('fast');
$(document).unbind('click'); //Unbind click
});
});
$('.about').click(function (e) {
e.stopPropagation();
});
Only remove the class active instead of toggling it:
jQuery(document).click(function(){
jQuery('.about-btn').removeClass( "active");
jQuery('.about').slideUp('fast');
});
If i understood correctly, you never need to set the class active when clicking outside the div, so it should do the trick
Instead of registering the click event to document, you could have an overlay under the popup div that appears together and have the close event register on that instead.
Or you could also store the state in a variable like so:
var isOpen = false;
jQuery('.showmenu').click(function(e){
e.stopPropagation();
jQuery('.about-btn').toggleClass( "active");
jQuery('.about').slideToggle('fast');
isOpen = true;
});
jQuery('.about').click(function(e){
e.stopPropagation();
});
jQuery(document).click(function(){
if(!isOpen) return; // early return if not opened
jQuery('.about-btn').toggleClass( "active");
jQuery('.about').slideUp('fast');
});

automatic close function for selection

I have a button which creates a pulldown in which you can select several categories.
Now i want this to close automatically when i click outside the pulldown.
Something like a lightbox or modal popup which closes if you click anywhere else on the page.
Now i have to click the button again to close it. If i dont and go elsewhere on the page, the dropdown stays visible (until i click it)
This is the code of the button:
$(function(){
$('#browse-btn').click(function(){
$('#browse-content').toggle('1000');
$(this).toggleClass('active');
if ($(this).hasClass('active')) $(this).find('span').html('▲')
else $(this).find('span').html('▼')
});
$(".scroll-top").scrollToTop();
$("#category_id").selectbox();
});
Is this possible?
thanks
Using jquery this is the code I used for a similar case scenario sometime ago:
$(document).click(function(event) {
if(!$(event.target).closest('.pulldown').length) {
if($('.pulldown').is(":visible")) {
$('.pulldown').slideUp()
}
}
})
You can read more about this in the original post How to detect a click outside an element? submitted by Art.
I'm not exactly sure of the elements you want to hide as you don't have a demo, so I cannot provide a fully working code, however you should do something like this:
$("body").click(function(event) {
if (event.target.id != "browse-btn") {
// Do something when there's a click outside of #browse-btn
// and the element you want to hide is currently visible
}
});
You can attach a click event to all chidren of the body tag that removes that active class, but you would want to make sure to unbind that event so it doesn't run every time a click takes place that doesn't have some sort of prevent default on it. Something like this:
$(function(){
var hidePulldown = function(){
$('#browse-btn').removeClass('active');
$('body *').unbind("click", hidePulldown);
}
$('#browse-btn').click(function(){
$('#browse-content').toggle('1000');
$(this).toggleClass('active');
if ($(this).hasClass('active')) $(this).find('span').html('▲')
else {
$(this).find('span').html('▼');
$(document).on('click', 'body *', hidePulldown);
}
});
$(".scroll-top").scrollToTop();
$("#category_id").selectbox();
});
Also, the
$(document).on('click', element, function(){function body})
is the preferred way to attach click events i believe: $(document).on('click', '#id', function() {}) vs $('#id').on('click', function(){})
This is what worked flawlessly for me after reading some of the answers here:
$(document).click(function(event) {
if(!$(event.target).closest('#menucontainer').length &&
!$(event.target).is('#menucontainer')) {
if($('#menucontainer').is(":visible")) {
$('#menucontainer').hide();
}
}
})
Thanks for pointing me in the right way!

jQuery if body hasClass then sidemenu toggleClass issue

What I'm trying to achieve is toggling the sidemenu on click anywhere in the body except inside sidemenu div itself. I currently can toggle the sidemenu from its toggle link (comment out line 13-14 in js fiddle) but not by clicking anywhere on body. This is the concerned code:
$('body').bind('click', function (e) {
var opened = $('body').hasClass('sidemenu-open');
if (opened === true) {
$('.sidemenu').removeClass('is-visible');
$('body').removeClass('sidemenu-open');
}
});
JSFIDDLE
I know I can add a wrapper for the content but I don't have much flexibility in the HTML structure.
There are two problems. First, when you click on the link, the click is bubbling out to the body. So the menu toggles open, then it toggles closed again, so nothing happens. Use event.stopPropagation() to prevent that.
Second, when the side menu is closed, <body> is just the one line containing the link. If you want to be able to click anywhere in the window, use $(document) rather than $('body').
$(function () {
$('.sidemenu-toggle').on('click', function (event) {
event.preventDefault();
event.stopPropagation();
$('.sidemenu').toggleClass('is-visible');
$('body.sidemenu-enabled').toggleClass('sidemenu-open');
$('.sidemenu').toggleClass('sidemenu-open');
});
});
$(document).on('click', function (e) {
var opened = $('body').hasClass('sidemenu-open');
if (opened === true) {
$('.sidemenu').removeClass('is-visible');
$('body').removeClass('sidemenu-open');
}
});
DEMO
Your click on the side menu is propagating down to the body and activating that handler as well - preventDefault isn't what you're after, you need to return false as well. Try this click handler instead.
$(function () {
$('.sidemenu-toggle').on('click', function (event) {
$('.sidemenu').toggleClass('is-visible');
$('body.sidemenu-enabled').toggleClass('sidemenu-open');
$('.sidemenu').toggleClass('sidemenu-open');
return false;
});
});
You can get the target element in click event by which you can have further conditions.
$('body').click(function(evt){
if(evt.target.id == "your_sidemenu_id"){
//do nothing
}
else{
//do the toggle here as this will be implemented whenever any element is clicked other than your sidemenu
}
});

jQuery - hide dropdown when DOM element is focused

i got to show a dropdown menu, now i would like to hide that when anoher element (not dropdown or dropdown's children) in the DOM is focused.
(hide dropdpown when element !== dropdown||dropdown's childrens is focused in the DOM)
i tryed with focusout() with no results:
$('a').on('click',function(){
$('.drop.user-menu').fadeIn();
});
$('.drop.user-menu').on('focusout',function(){
$(this).fadeOut();
alert('antani');
});
any idea?
jsfiddle here : example
event.target() will be useful in this scenario:
$('.drop.user-menu').on('focusout',function(e){
if(e.target !== this){
$(this).fadeOut();
alert('antani');
}
});
Update:
Check this out and see if helps:
$('.a').on('click', function (e) {
e.stopPropagation();
$('.drop.user-menu').fadeToggle();
});
$('.drop.user-menu').on('click', function (e) {
e.stopPropagation();
$('.drop.user-menu').fadeIn();
});
$(document).on('click', function (e) {
if (e.target !== $('.drop.user-menu') && e.target !== $('.a')) {
$('.drop.user-menu').fadeOut();
}
});
The above script done with click in this fiddle
A DIV cannot take or lose focus (unless it has a tabindex). You'll have to give it a tabindex or add a focusable element into your div.drop.user-menu. See Which HTML elements can receive focus?.
You then also have to explicitly give that element (or an element within it) focus (with .focus()) as simply fading it in doesn't give it focus.
When the element blurs, then check if the new active element is still part of the menu. If it's not, fade out the menu.
See a working example.
There is no focus or focusout events triggered, because you're not operating on form fields.
This is probably what you want : How do I detect a click outside an element?
var menu = $('.drop.user-menu');
menu.on('click',function(e){
e.stopPropagation(); // stop clicks on menu from bubbling to document
});
$('a').on('click', function (e) {
menu.fadeIn();
e.stopPropagation(); // stop clicks on <a> from bubbling to document
});
$(document).on('click',function(e){
// any other click
if (menu.is(":visible")) {
menu.fadeOut();
}
});
http://jsfiddle.net/BBxEN/10/
Update
As Derek points out, this is not very friendly for keyboard users. Consider implementing a way for users to both open and close the menu using keyboard shortcuts.
You can tru with blur, is what you want?
Try this:
$('.drop.user-menu').on('blur',function(){
$(this).fadeOut();
alert('antani');
});

Dropdown More errors

I have a script here that if i click on it drops down, as well on if i clickoutside it drops down. But i want to make it so that if i click on bottom it scrolls up. For some reason it doesnt work here. It only scrolls up if i click outside the box.
Heres an example. http://jsfiddle.net/w8mfx/
If i click on bottom it wont scroll up only if i click on outside. But i want it to work both ways.
$(document).ready(function() {
$('#bottom').click(function(event) {
event.stopPropagation();
$('#content').slideDown();
});
$('html').click(function(e) {
//alert(1);
if (e.target.id != 'bottom') {
$('#content').slideUp();
}
});
});
http://jsfiddle.net/w8mfx/7/
I added:
$('#content').click(function (event) {
event.stopPropagation();
});
so the user can click on the #content element and not trigger the slide function. That also means that the event handler for the html element can be simpler:
$('html').click(function(e) {
$('#content').slideUp();
});
In the jsfiddle you may notice I cached the $('#content') selector in a variable since it was being used in more than one place.
Use slideToggle(). http://jsfiddle.net/w8mfx/5/

Categories

Resources