I added the code to the js below. When you click on either the Sign up or Login the arrow moves / point up, but when you close it again it stay that way. Can someone help me out figure what to do.
I tried using this post but cant make this work either here
Here is the Jquery script I use
$(document).ready(function () {
$('#login-trigger').click(function () {
$('#login-content').slideToggle();
$(this).addClass('active');
if ($(this).hasClass('active')) $(this).find('span').html('▲');
$('#signup-content').slideUp();
})
$('#signup-trigger').click(function () {
$('#login-content').slideUp();
$(this).addClass('active');
if ($(this).hasClass('active')) $(this).find('span').html('▲');
$('#signup-content').slideToggle();
});
});
Here is my jsfiddle, ( I know some people don't pref jsfiddle but since I have a lot of code and it will be so much easier to show what I'm trying to do with it)
the problem is since you are adding the class active to it, on click event .. and right after, you are checking if it has a class active(which is always true)...
use toggleClass(it toggles the mentioned class) instead of addClass and check with hasClass
$(document).ready(function () {
$('#login-trigger').click(function () {
$('#login-content').slideToggle();
$(this).toggleClass('active');
if ($(this).hasClass('active')){
$(this).find('span').html('▲');
}else{
$(this).find('span').html('▼');
}
$('#signup-content').slideUp();
});
.... //same for signup
fiddle here
and yes I know some people don't pref jsfiddle .. i guess you are wrong , i am sure most people prefer jsfiddle rather than the code itself since it is easier to test in fiddle.
You missed else part.
Try:
$(this).toggleClass('active');
if ($(this).hasClass('active')){
$(this).find('span').html('▲');
}
else{
$(this).find('span').html('▰');//code of down arrow
}
Fiddle here.
You never remove the class active, so after the first click it looks like it is always active, and you also never change the triangle shape after the first click.
This code should work for the login:
$('#login-trigger').click(function () {
$('#login-content').slideToggle();
if (!$(this).hasClass('active')) { //Now is not active, I'll make it active
$(this).addClass('active'); //Add class
$(this).find('span').html('▲'); //BLACK UP-POINTING TRIANGLE
} else { //Now is active, I'll make it not active
$(this).removeClass('active'); //Remove class
$(this).find('span').html('▼'); //BLACK DOWN-POINTING TRIANGLE
}
$('#signup-content').slideUp();
})
This is the jsfiddle link.
Related
I don't know if it's me or there's really something strange here, but I can't remove the class active from all buttons (third line of the following code). All buttons keep the class active.
Any idea ? Am I to hungry to code ? Thanks for you help 😅
$(document).on('click', '#filters button', function (event) {
jQuery('#unit-selector svg .active').removeClass('active');
jQuery('#filters button .active').removeClass('active');
jQuery(this).addClass('active');
// Get Size Value
var size = jQuery(this).attr('id');
if (size == "floor-show-3") {
jQuery('#unit-selector svg .three').addClass('active');
}
if (size == "floor-show-4") {
jQuery('#unit-selector svg .four').addClass('active');
}
if (size == "floor-show-5") {
jQuery('#unit-selector svg .five').addClass('active');
}
});
If you want to remove active class from all buttons, you need to use:
$('#filters button.active').removeClass('active');
$(this).addClass('active');
Or, you can also do the above two-line logic in one-line like
$(this).addClass('active').siblings().removeClass('active');
Please note this one-line logic will only work if the buttons are next to each other like:
<button>Add</button>
<button>Edit</button>
<button>Cancel</button>
Your usage of removeClass seems okay, but I can't be sure that your selector is correctly selecting your buttons without seeing the button markup. Can you share that as well?
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!
I have fixed sidebar navigation bar, it works on hover but I want to open first menu by clicking on collapse button. similar working like hover on menu 1. I have already tried following methods .
jsfiddle Demo
$(document).on('click', '.btn1', function () {
console.log('btn 1');
//$('.imFirst a').trigger('mouseenter');
//$('.imFirst a').trigger('mouseover');
//$('.imFirst a').trigger('hover');
$('.imFirst a ').mouseenter()
//$('.imFirst a ').mouseover()
$('.imFirst a').toggleClass('hover');
});
$(document).on('click', '.btn2', function () {
console.log('btn 2');
//$('.imFirst ').trigger('mouseenter');
//$('.imFirst ').trigger('hover');
//$('.imFirst ').trigger('mouseover');
$('.imFirst ').mouseenter();
//$('.imFirst ').mouseover();
$('.imFirst ').toggleClass('hover');
});
You must add class, to rember your condition. I am change your example
jsfiddle Demo
$(document).on('click', '.btn2', function () {
$('.imFirst').addClass('active');
});
Like the previous answer I think your best bet is to add a class that is toggled when you click on the toggle button. The only difference with the previous answer is in the jsfiddle demo it works much better to add
$('.imFirst').toggleClass('active');
so you are able to close the menu after opening it.
My rep is too low to add this as a comment.
I am very new in programming, please give me a mercy. Below is my code:
$(function(){
document.getElementById("custom_link").addEventListener("mouseover",function(){
document.getElementById("siteContent").contentDocument.getElementById("custom_div").classList.toggle('highlightDiv');
},false)})
$(function(){
document.getElementById("custom_link").addEventListener("click",function(){
document.getElementById("siteContent").contentDocument.getElementById("custom_div").classList.add('highlightDiv');
},false)})
What I want to do is:
when the user hovers mouse on "custom_link", the "custom_div" is being highlighted.
when the user moves mouse out off "custom_link", the highlight at "custom_div" is eliminated.
when the user clicks at "custom_link", "custom_div" is being highlight again. However, when the user moves mouse out, the 'highlightDiv' is still being added to "custom_div".
According to my code, it does not work properly because the behavior when hovering is strange. It would be very nice if you can explain me with full code structure or jsfiddle example. Thank you for your advance help.
http://jsfiddle.net/ETrjA/2/
$('#custom_link').hover(function () {
$('#custom_div').toggleClass('highlighted');
});
$('#custom_link').click(function (e) {
$('#custom_div').addClass('highlighted');
$(e.currentTarget).unbind('mouseenter mouseleave');
});
You only need one class highlighted and you can access the link element directly within the click event callback via e.currentTarget.
You are mixing Javascript with its framework jQuery. Stick with jQuery for this.
// CSS: Create the highlight accessible with two classnames.
.highlight, .highlight_stay{
background:yellow;
}
Jquery
$(function(){
$('.custom_link').hover(function(){
$(this).addClass('highlight');
}, function(){
$(this).removeClass('highlight');
});
$('.custom_link').click(function(){
$(this).addClass('highlight_stay');
});
});
here is a link http://jsfiddle.net/8GV7B/2/
$(function(){
mouse_is_clicked = false;
$(".custom_link").hover(function(){
$("#container").addClass("highlight");
}, function(){
if(!mouse_is_clicked){
$("#container").removeClass("highlight");
}else{
mouse_is_clicked = false;
}
});
$(".custom_link").click(function(){
$("#container").addClass("highlight");
mouse_is_clicked = true;
});
});
I'm trying to get this to work:
if($('#myDiv').hover() == false) {
myFunction();
}
Not getting much in the way of errors in Chrome or Firebug consoles. I've had a look at some other posts, and there was an answer that used something like:
if($('#myDiv').is(':hover') == false) {
myFunction();
}
However this also doesn't work.
Here's a jsfiddle if that helps: http://jsfiddle.net/yuwPR/2/
Any ideas greatly appreciated.
Thanks
EDIT:
Thanks for the answers, I wasn't able to get anything working. I'm thinking it might not be possible. Oh well, I'll try something else!
Thanks again
p.s. Most inventive answer marked as right and upvotes all round.
Without knowing your ultimate intent, you could wire up a hover on the document and check the current target.id
$(document).mouseover(function(event) {
if (event.target.id == "myDiv") {
$("body").css("background-color", "red"); //over the div so change the color
return;
}
$("body").css("background-color", "green"); //no on the div
});
code example on jsfiddle.
This code sample sets up a global js variable to store the hover state of the div. Then I use jquery hover to toggle that between true / false. Then, we just fire off a function every 10ms that checks the hover state. Currently I am just setting the window status telling you if you're hovered or not.
var _MOUSEOVER_IN_PROGRESS = false; //stores the hover state
function isover(){
if(_MOUSEOVER_IN_PROGRESS){
window.status = 'Still over!';
} else {
window.status = 'You are not hovering on me!';
}
setTimeout("isover()",10); //checking every 10ms!
}
$(document).ready(function(){
isover();
$('#mydiv').hover(
function(){ _MOUSEOVER_IN_PROGRESS = true; },
function(){ _MOUSEOVER_IN_PROGRESS = false; }
);
});
Edited my code! My mydiv hover catch was not wrapped in a document ready
The hover function takes 2 callback functions:
('#myDiv').hover(function () {
// function to call when hovering
},
function () {
myFunction();
}
);
So, when hovering is "false", ie, on mouse out, the second function will be called.
If you're only interested in doing something when the hover stops, you can use the mouseout() function:
$('#myDiv').mouseout(function() {
myFunction();
}
);
Your first call could never work:
$('#myDiv').hover()
This actually says "trigger the hover event on the element". It does not check to see if your user is currently hovering over the element.
Your second formulation should work:
$('#myDiv').is(':hover')
This checks to see if the element currently has the mouse hovering over it. However, it doesn't seem to work on document load. An example that works can be seen here. If you can clarify what you're trying to do, it might be possible to find some working code in this style.