How to Toggle Mouse Hover Event on Click? - javascript

I want to change the color of element on Hover. However I want to disable hover effect while clicking on the element and set the clicked element red. Again if anyone clicks on the element , and I want to enable the Hover effect and apply the hover effect.
$('.divElement').on('mouseenter', function () {
$(this).addClass('red');
});
$('.divElement').on('mouseleave', function () {
$(this).removeClass('red');
});
$('.divElement').on('click', function () {
$(this).removeClass('red');
$(this).off('mouseenter mouseleave');
});
I Have tried this jQuery Code.
<div class="divElement">Element 1</div>
<div class="divElement">Element 2</div>
<span class="divElement">Element 3</div>
<div class="divElement">Element 4</div>
.divElement {
color: blue;
}
.divElement.red {
color: red;
}

What you have tried is just to disable (unbind) the Hover event. What You actually need is to toggle the hover event if it is clicked.
First of all, I would like to suggest you, to change <span> tag to <div> or add CSS for class .divElemenT{ display:block;} else inline-block element and block element may hover at once.
var hoverEvent= true;
$(".divElement").hover(
function() {
if(hoverEvent) $(this).toggleClass("red");
}
);
$('.divElement').click(function() {
$(this).toggleClass('selected');
$('.divElement').not(this).removeClass('selected,red');// remove this line if you want multiple selector
hoverEvent= !hoverEvent;
});
.divElement {
color: blue;
display:block;
}
.divElement.red {
color: red;
}
.selected{
font-weight:bold;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="divElement">Element 1</div>
<div class="divElement">Element 2</div>
<span class="divElement">Element 3</span>
<div class="divElement">Element 4</div>
Please leave a comment if it doesn't work.

I suggest you create a hover class which indicates that the class is hoverable. Then in your jQuery you can target elements with .hover class for your mouseenter and mouseleave leave events. This means instead of turning off the event listeners for your elements you can instead simply toggle the hover class for each element.
See working example below:
$(document).on('mouseenter', '.divElement.hover', function() {
$(this).addClass('red');
});
$(document).on('mouseleave', '.divElement.hover', function() {
$(this).removeClass('red');
});
$('.divElement').on('click', function() {
$(this).toggleClass('hover');
});
.divElement {
color: blue;
}
.divElement.red {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="divElement hover">Element 1</div>
<div class="divElement hover">Element 2</div>
<div class="divElement hover">Element 3</div>
<div class="divElement hover">Element 4</div>
Note: You need to use $(document).on as you are dynamically changing the classes after the event biding initially occurs, allowing you to listen to events on elements dynamically added/changed.

try this
$('.divElement').click(function(){
if($(this).hasClass("red")||$(this).hasClass("selected")){
$('.divElement').toggleClass("red");
$(this).toggleClass("selected");
}
});
.red:hover,.selected{
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="divElement red">Element 1</div>
<div class="divElement red">Element 2</div>
<span class="divElement red">Element 3</span>
<div class="divElement red">Element 4</div>

Use mousedown event. Click responds after completing the click, mousedown works in between the click completion.
$('.divElement').on('mouseenter', function () {
$(this).addClass('red');
});
$('.divElement').on('mouseleave', function () {
$(this).removeClass('red');
});
$('.divElement').on('mousedown', function () {
$('.divElement').removeClass('red');
$(this).addClass('red');
$('.divElement').off('mouseenter mouseleave');
});
.divElement {
color: blue;
}
.divElement.red {
color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="divElement">Element 1</div>
<div class="divElement">Element 2</div>
<span class="divElement">Element 3</span>
<div class="divElement">Element 4</div>

Your approach by toggle class "red" is correct. I just change a bit
HTML
<div class="divElement red">Element 1</div>
<div class="divElement red">Element 2</div>
<div class="divElement red">Element 3</div>
<div class="divElement red">Element 4</div>
CSS
.divElement {
color: blue;
}
.divElement.red:hover, .divElement.selected {
color: red;
}
JQUERY
$('.divElement').on('click', function () {
$(this).toggleClass('red selected');
});
The idea is only make the hover event in class "red", then we toggleClass red for element when clicked.

Related

Is it possible to select a element to hover it and it is not it's child with CSS only?

I have an <aside> and <header> and the header has child called container and cont. has some children one of them is called burger bar so what I want here is when I :hover the burger bar the <aside> will be visible so I wonder how to do that or if it is impossible I tried to do this header .container .burger_bar:hover + aside but the aside is not element beside the burger_bar so it's not going to work.
more explain...
<div class='header'>
<div class='container'>
<div class='burger_bar'>...</div>
</div>
</div>
<aside>...</aside> /* <---when hovering the burger bar this will be
transform: translate(0%) right after being transform: translate(-100%) */
If trigger and target are on the same level, you can use .trigger:hover ~ .target to target your .target element while .trigger is being hovered.
.trigger:hover ~ .target {
color: green;
}
<div class="trigger">Hover Me</div>
<div class="target">Target</div>
If your trigger and target are not on the same level, it's better to use some javascript to add a class to your target.
const trigger = document.querySelector('.trigger');
const target = document.querySelector('.target');
trigger.addEventListener('mouseover', () => {
target.classList.add('active');
});
trigger.addEventListener('mouseleave', () => {
target.classList.remove('active');
});
.target.active {
color: green;
}
<div class="parent1">
<div class="trigger">Hover Me</div>
</div>
<div class="parent2">
<div class="target">Target</div>
</div>
Or you could use :has() pseudo class but be aware if its poor coverage (only works in Safari right now)
.parent1:has(.trigger:hover) ~ .parent2 .target {
color: green;
}
<div class="parent1">
<div class="trigger">Hover Me</div>
</div>
<div class="parent2">
<div class="target">Target</div>
</div>

How I hide a div when jquery show another div?

I want that when the click activate the element2 div, the element should disappear. And the element2 div should not appear at the beginning.
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").hide();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">
Element 1
<div class="toggle">
toggle
</div>
<div class="element2">
Element 2
<div class="close">close Element 2</div>
</div>
</div>
Add display none to hide an element from the start:
<div class="element2" style="display:none">
The rest of your code appears to be doing what it's supposed to, unless I am misunderstanding "I want that when the click activate the element2 div, the element should disappear"... which is entirely possible.
In order to have element2 hidden at the beginning you need to either add a style tag or even better add a CSS file where you will keep all of your stylings in one place.
For style tag:
<div class="element2" style="display:none">
For CSS:
.element2 {
display: none;
}
Then for your code you are close. In order to make element hide, you need to change it to:
$(".toggle").click(function() {
$(".element2").show();
$(".element").hide();
});
$(".close").click(function() {
$(".element2").hide();
$(".element").show();
});
The HTML will need some changes to, this will make what I wrote work the way I believe you want it to:
<div class="element">
Element 1
<div class="toggle">
toggle
</div>
</div>
<div class="element2">
Element 2
<div class="close">close Element 2</div>
</div>
You should probably do something like this:
$(".toggle").click(function() {
$(this).parent().find(".element2").toggle();
});
$(".close").click(function() {
$(this).parent().hide(); // close the correct .element2
});
In CSS you need to:
.element2 {
display: none;
}
just $(".element2").hide(); hide it at start
$(function() {
$(".element2").hide();
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").hide();
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="element">Element 1
<div class="toggle">Toggle </div>
<div class="element2"> Element 2
<div class="close"> close</div>
</div>
</div>
HTML
<div class="element">
<div class="toggle"></div>
<div class="element2" style="display:none">
<div class="close"></div>
</div>
</div>
EXAMPLE CSS
.toggle
{
display:block;
width:20px;
height:20px;
margin-left:10px;
float:left;
background:green;
}
.element2{
display:block;
width:40px;
height:40px;
margin-left:10px;
float:left;
background:yellow;
}
.close{
display:block;
width:20px;
height:20px;
margin-left:10px;
float:right;
border:1px solid #000;
}
JQUERY
$(".toggle").click(function() {
$(".element2").toggle();
});
$(".close").click(function() {
$(".element2").css({"display":"none"});
});
fiddle to check
I hope it is helpfull answer. Good Luck !

Change Div Class on click

I'm trying to change Div class on click. What I'm currently trying to do:
I got this on my divs:
onclick"changeClass()"
and this is my function
function changeClass(){
$("#test123").attr("class", "classname");
}
However it's not working. You should now that I generate many divs with Id test123 with foreach loop so that may be the problem but I'm not really sure
You forgot the =:
onclick="changeClass()"
Also, you need to specify which?
onclick="changeClass(this)"
Then in the code:
function changeClass(which) {
$(which).attr("class", "classname");
}
Instead of all these, since you are using jQuery, you can do it in a simple way:
$(function () {
$("div").click(function () {
this.className = "classname";
});
});
.classname {
background: #ccf;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test1">Test 1</div>
<div id="test2">Test 2</div>
<div id="test3">Test 3</div>
<div id="test4">Test 4</div>
If you want multiple classes, it is always better to add and remove class.
$(function () {
$("div").click(function () {
$(this).addClass("classname");
});
});
.classname {
background: #ccf;
}
.myClass {
font-weight: bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="test1">Test 1</div>
<div id="test2" class="myClass">Test 2</div>
<div id="test3">Test 3</div>
<div id="test4" class="myClass">Test 4</div>
This way, it doesn't affect the previous classes.
https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XUL/Attribute/onclick
onclick
Type: script code
This event handler is called when the object is clicked.
Example
< image src="hello.png" onclick="alert('Hi')"/>
As described on example, it should be written as onclick=foo(args)
changeClass = function(){
document.querySelector("div").setAttribute('class', 'black');
}
.red {
background: red;
}
.black {
background: black;
}
<div class="red" onclick="changeClass()">
lorem
</div>
<br>
<button onclick="changeClass()">click me to change class</button>

Closing all other DIVS while opening one

What I am trying to achieve is the following
There are two DIVS with dropdown. I need to close one while opening the other on click function.
I am also trying to mouseout once the event is out of the dropdown box.
I would like to close the DIV once the click even happens outside the dropdown box.
Following is the HTML
<div class="first-div" style="display:inline-block">
<a class="first-div-link"><h6>REGION</h6></a>
<div class="first-div-dropdown">
<p>Drop down test from first DIV</p>
</div>
</div>
<div id="second-div" style="display:inline-block; float:right">
<h6>REGISTER</h6>
<div class="second-div-dropdown">
<p>Drop down test from second DIV</p>
</div>
</div>
CSS is following
.first-div-dropdown, .second-div-dropdown{
background-color:#555;
color:white;
height:100px;
width:200px;
}
JS is following
$(document).ready(function(){
$('.first-div-dropdown').hide();
$('.second-div-dropdown').hide();
$('.first-div-link').on('click', function (event){
$('.first-div-dropdown').slideDown(300);
});
$('.second-div-link').on('click', function (event){
$('.second-div-dropdown').slideDown(300);
});
});
Is there any way to use this as a function to control multiple DOMs in the HTML? If so could someone assist me with the current example ?
Thanks
The path to follow here is use a common class on your items, you don't need to create new classnames if all will have the same styles and will perform the same action. Check this:
$(document).ready(function() {
$('.cont-div').on('click', 'a', function(e) {
e.preventDefault();
$('.div-dropdown').slideUp(300);
$(this).next('.div-dropdown').stop().slideToggle(300);
});
//To close if you click outside the container divs
$('body').on('click', function(e) {
if (!$(e.target).parents('.cont-div').length) {
$('.div-dropdown').slideUp(300);
}
})
});
body {
height: 600px;
background: #e1e1e1;
}
.cont-div {
display: inline-block;
vertical-align: top;
width: 50%;
}
.div-dropdown {
background-color: #555;
color: white;
height: 100px;
width: 200px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="cont-div">
<h6>REGION</h6>
<div class="div-dropdown">
<p>Drop down test from first DIV</p>
</div>
</div><!--
--><div class="cont-div">
<h6>REGISTER</h6>
<div class="div-dropdown">
<p>Drop down test from second DIV</p>
</div>
</div>
If you want to get more specific, you could assign a similar class to both menus, in the case below, I added 'dropdown-div' to the class for both menus and then simply added a trigger whenever you click on something that is not a link, it will hide the menus by calling $('.dropdown-div').hide();
$(document).ready(function(){
$('.first-div-dropdown').hide();
$('.second-div-dropdown').hide();
$('.first-div-link').on('click', function (event){
$('.first-div-dropdown').slideDown(300);
});
$('.second-div-link').on('click', function (event){
$('.second-div-dropdown').slideDown(300);
});
});
$(document).on('click', function(event) {
if (!$(event.target).closest('a').length) {
$(".dropdown-div").hide();
}
});
.first-div-dropdown, .second-div-dropdown{
background-color:#555;
color:white;
height:100px;
width:200px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div class="first-div " style="display:inline-block">
<a class="first-div-link"><h6>REGION</h6></a>
<div class="first-div-dropdown dropdown-div">
<p>Drop down test from first DIV</p>
</div>
</div>
<div id="second-div" style="display:inline-block; float:right">
<h6>REGISTER</h6>
<div class="second-div-dropdown dropdown-div">
<p>Drop down test from second DIV</p>
</div>
</div>
You're dealing with state management within a collection. You have 2 dropdowns, but 3 states: dropdown one's state, dropdown two's state, and the collection of dropdowns' state.
Using jQuery, the most common way of handling this I've seen is to start by "resetting" the collection's state each time, by hiding all dropdowns on click.
Then, open the dropdown that is being targeted by the client. This can also be a bit easier if you use a single class to target the collection which also lends itself to be reusable across an infinite number of dropdowns.
<div class="first-div" style="display:inline-block">
<a class="dropdown-trigger"><h6>REGION</h6></a>
<div class="dropdown-menu">
<p>Drop down test from first DIV</p>
</div>
</div>
<div id="second-div" style="display:inline-block; float:right">
<a class="dropdown-trigger"><h6>REGION</h6></a>
<div class="dropdown-menu">
<p>Drop down test from second DIV</p>
</div>
</div>
JS:
$('.dropdown-trigger').click(function (event) {
event.stopPropagation();
var $menu = $(this).siblings('.dropdown-menu');
$('.dropdown-menu').not($menu).slideUp(300);
$menu.slideToggle(300);
});
$(document).click(closeDropdowns);
function closeDropdowns () {
$('.dropdown-menu').slideUp(300);
}
Working codepen: http://codepen.io/amishstripclub/pen/wzbEVo
You could try using toggle like this:
$(document).ready(function(){
$('.first-div-link').on('click', function (event){
$('.first-div-dropdown').toggle();
$('.second-div-dropdown').toggle();
});
$('.second-div-link').on('click', function (event){
$('.first-div-dropdown').toggle();
$('.second-div-dropdown').toggle();
});
});

jQuery - change element height if other element becomes visible

I have 3 buttons that toggleFade() 3 divs. When i click #link1, the div1 fadeIn() and so on..
My goal is to resize #map_canvas if any of these divs are faded in, and resize to default if none are visible (fadeOut()).
<a id="link1"></a>
<a id="link2"></a>
<a id="link3"></a>
<div id="map_canvas"></div>
<div id="wrapper">
<div id="div1" class="hideMe"></div>
<div id="div2" class="hideMe"></div>
<div id="div3" class="hideMe"></div>
</div>
EDIT: jQuery of fadeIn and fadeOut.
$(document).ready(function() {
$('#div1').hide();
$('a#link1').click(function() {
if (!$('#div1').is(':visible'))
{
$('.hideMe').fadeOut("slow");
$('#map_canvas').animate({height:"370px"}, 500);
}
$('#div1').fadeToggle("slow");
});
Well as much as I could understand I implemented an example. I only did the first two buttons you can use the first two buttons as example to implement the third.
Note: Its possible to consolidate the jQuery so that there is only one click function however while learning Its helpful to keep it separated so its more understandable.
HTML
<a id="link1">link1</a>
<a id="link2">link2</a>
<a id="link3">link3</a>
<div id="map_canvas"></div>
<div id="wrapper">
<div id="div1" class="hideMe">div1</div>
<div id="div2" class="hideMe">div2</div>
<div id="div3" class="hideMe">div3</div>
Javascript/Jquery
$(document).ready(function() {
$('.hideMe').hide();
$('#link1').click(function() {
$('.hideMe').not('#div1').hide();
$('#div1').fadeToggle("slow",function(){
if ($('#div1').is(':visible'))
{
$('#map_canvas').animate({height:"370px"}, 500);
}
if(!$('.hideMe').is(':visible')){
$('#map_canvas').animate({height:"0px"}, 500);
}
});
});
$('#link2').click(function() {
$('.hideMe').not('#div2').hide();
$('#div2').fadeToggle("slow",function(){
if ($('#div2').is(':visible'))
{
$('#map_canvas').animate({height:"370px"}, 500);
}
if(!$('.hideMe').is(':visible')){
$('#map_canvas').animate({height:"0px"}, 500);
}
});
});
});
CSS
#map_canvas {
border: 1px solid black;
}
a {
cursor:pointer;
}
Fiddle can be found here
http://jsfiddle.net/qYys7/

Categories