I am trying to create a link <a> with two different backgrounds, one a "Down Arrow" and one an "Up Arrow" depending on the classname arrow-up and arrow-down.
So when you click on the down arrow, the div named product-add-wrapper slides down, and the down arrow* becomes an **up arrow and vice versa.
The problem is, the .toggle + callback seems to work fine, it adds the desired class name and removes the desired class name, however, the background-image doesn't change (the down arrow doesn't become an up arrow).
Here is the html.
<span class="arrow-right">
<a class="updown arrow-down"> </a>
</span>
Here is the css.
.updown {
display: block;
margin-top: -1px;
height: 25px;
width: 25px;
}
.arrow-up {
background-image: url('../img/arrow-up.png');
}
.arrow-down {
background-image: url('../img/arrow-down.png');
}
And here is the javascript.
$('.updown').click(function() {
$('#product-add-wrapper').toggle('slow', function() {
var classname = $('.updown').attr('class');
if (classname === 'up arrow-down') {
$('.updown').removeClass('arrow-down').addClass('arrow-up');
} else {
$('.updown').removeClass('arrow-up').addClass('arrow-down');
}
});
});
Any ideas? Thanks in advance.
The classname will never be 'up arrow-down' you probably meant 'updown arrow-down'.
if (classname === 'up arrow-down') {
This would probably be better rewritten like this:
$('.updown').click(function() {
$('#product-add-wrapper').toggle('slow', function() {
if ($('.updown').hasClass('arrow-down')) {
$('.updown').removeClass('arrow-down').addClass('arrow-up');
} else {
$('.updown').removeClass('arrow-up').addClass('arrow-down');
}
});
});
Or better yet:
Js:
$('.updown').click(function() {
$('#product-add-wrapper').toggle('slow', function() {
$('.updown').toggleClass('arrow-up');
});
});
CSS:
.updown {
display: block;
margin-top: -1px;
height: 25px;
width: 25px;
background-image: url('../img/arrow-down.png');
color:green;
}
.arrow-up {
background-image: url('../img/arrow-up.png');
color:red;
}
HTML:
<span class="arrow-right">
<a class="updown"> aa</a>
</span>
<div id="product-add-wrapper">
aa
</div>
Jsfiddle: http://jsfiddle.net/kingmotley/K7274/1/
Here it is using the .click() event handler instead of toggle:
$('.updown').click(function (){
if ($(this).hasClass("arrow-down")){
$('#product-add-wrapper').slideUp("slow");
$(this).removeClass('arrow-down').addClass('arrow-up');
} else {
$('#product-add-wrapper').slideDown("slow");
$(this).removeClass('arrow-up').addClass('arrow-down');
};
});
HTML as follows:
<span class="arrow-right">
<a class="updown arrow-down"> </a>
</span>
<div id="product-add-wrapper">
... DIV CONTENT ...
</div>
Working demonstration: http://jsfiddle.net/9wxfJ/3/
This should work, using hasClass:
$('.updown').click(function() {
$('#product-add-wrapper').toggle('slow', function() {
if($('.updown').hasClass('arrow-down')) {
$('.updown').addClass('arrow-up');
$('.updown').removeClass('arrow-down');
} else {
$('.updown').addClass('arrow-down');
$('.updown').removeClass('arrow-up');
}
});
});
Fiddle: http://jsfiddle.net/RP294/
Personally, I'd go about this using one class instead of two since you know the element you're working with has a default state. So instead of having an .arrow-down class, just give that background to the .updown class and have the .arrow-up class use !important to overrule it when toggled:
HTML
<span class="arrow-right">
<a class="updown"> </a>
</span>
CSS
.updown {
display: block;
margin-top: -1px;
height: 25px;
width: 25px;
background-image: url('../img/arrow-down.png');
}
.arrow-up {
background-image: url('../img/arrow-up.png') !important;
}
This will also shorten your javascript code quite a bit:
$('body').on('click', '.updown', function(e){
var arrow = $(this);
$('#product-add-wrapper').toggle('slow', function() {
arrow.toggleClass('arrow-up');
});
});
Work fine for me
http://jsfiddle.net/Hzmxc/
$('.updowns').click(function() {
$('#product-add-wrapper .updown').toggle('slow', function() {
if ($('.updowns').hasClass('arrow-down')) {
$('.updowns').removeClass('arrow-down').addClass('arrow-up');
} else {
$('.updowns').removeClass('arrow-up').addClass('arrow-down');
}
});
});
Related
My onmouseenter function isn't triggering.
I created a button that would allow you to go to the top. However, once there I want the button to have a display property of none. I tried doing it in two ways. One with classList and the other with style.display = 'none'. Both didn't work, am I missing something in the logic ?
EDIT-------------
onmouseleave the button should reappear. I added the function.
Here is a code pen
const topDiv = document.getElementById('topDiv')
const arrowup = document.getElementById('arrowup')
const hideArrow = () => {
if (topDiv) {
arrowup.classList.remove(showme');
arrowup.classlist.add('hideme');
} else {
arrowup.classList.add('showme');
}
}
const showArrow = () => {
if (!topDiv) {
arrowup.classList.remove('hideme');
arrowup.classList.add('showme');
}
}
#top {
height: 1000px;
}
a {
position: fixed;
top: 10px;
}
.showme {
display: block;
}
.hideme {
display: none;
}
<div onmouseleave="showArrow() onmouseenter="hideArrow()" id="top">
hello
</div>
<a class="showme" id="arrowup" onClick="hideArrow()" href="#top">
click me
</a>
There are some issues:
onmouseenter="hideArrow" is missing brackets -> onmouseenter="hideArrow()"
add and remove are functions, that get the class as param -> add('showme')
the return is wrong -> remove it
Working example:
const topDiv = document.getElementById('topDiv')
const arrowup = document.getElementById('arrowup')
const hideArrow = () => {
if (topDiv) {
arrowup.classList.remove('showme');
arrowup.classList.add('hideme');
}
else {
arrowup.classList.add('showme');
}
}
#top {
height: 1000px;
}
a {
position: fixed;
top: 50px;
}
.showme {
display: block;
}
.hideme {
display: none;
}
<div onmouseenter="hideArrow()" id="topDiv">
hello
</div>
<a class="showme" id="arrowup" onClick="hideArrow()" href="#topDiv">
click me
</a>
Remove the return keyword from the if condition because it causes the function to stop running at that point
element.classList.add() is a function while you are assigning classes to the element using the '=' operator. So the correct way to do it is arrowup.classList.add('hideme') and arrowup.classList.remove('showme').
I know that it might have been asked billion times, but I could not find any solutions. So, here is my code:
.btn {
width: 200px;
height: 45px;
}
.--selected {
background-color: red;
}
<div class="container">
<button class="btn --selected">uno</button>
<button class="btn">dos</button>
</div>
I need to make that after pressing button it added a --selected class to it, and another one lost it, as if it is a switch between two
Help me out, please
This will add the class to the button clicked and removes the class from all other elements with the class btn.
I changed the classname a little to make it a valid css class name too.
$('.btn').on('click', function() {
$(this).addClass('selected');
$('.btn').not(this).removeClass('selected');
});
.btn {
width: 200px;
height: 45px;
}
.selected {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<button class="btn">uno</button>
<button class="btn">dos</button>
</div>
You can try with removeClass() and toggleClass().
Please Note: --selected is not a valid class name. Double dash (--) is used to comment code in CSS. Though you can use like single, triple consecutive dashes, it is good practice to avoid those naming pattern.
$('.btn').click(function(){
$('.selected').removeClass('selected');
$(this).toggleClass('selected');
});
.btn {
width: 200px;
height: 45px;
}
.selected {
background-color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
<button class="btn selected">uno</button>
<button class="btn">dos</button>
</div>
This can be done easily using vanilla Javascript and ES6 as below, not need to add extra library:
const btns = Array.from(document.querySelectorAll('.btn'));
const toggleClass = (e) => {
for (var el of btns) {
el.classList.remove('--selected')
}
e.currentTarget.classList.add('--selected')
}
for (var el of btns) {
el.addEventListener('click', toggleClass)
}
.btn {
width: 200px;
height: 45px;
}
.--selected {
background-color: red;
}
<div class="container">
<button class="btn --selected">uno</button>
<button class="btn">dos</button>
</div>
this code might help you.
Simple:
$('.btn').click(function() {
$('.btn').removeClass('--selected');
$(this).addClass('--selected');
});
How to display the line when I hover over my div circle?
#line {
display: none
}
<div id='circle'>
<div id= 'line'>
Assuming you are using jQuery you can use:
var enterHandler = function(){
$("#line").show();
};
var leaveHandler = function(){
$("#line").hide();
};
$("#circle").hover(enterHandler, leaveHandler);
First thing, with your code, it is not clear if the <div>s are siblings are nested. I will give you the solution for both.
Nested
div {
padding: 10px;
background: #99c;
}
#line {
display: none;
background: #9c9;
}
#circle:hover #line {
display: block;
}
<div id='circle'>
<div id='line'>
</div>
</div>
Siblings
div {
padding: 10px;
background: #99c;
}
#line {
display: none;
background: #9c9;
}
#circle:hover + #line {
display: block;
}
<div id='circle'>
</div>
<div id='line'>
</div>
If you are using jQuery then you can simply try:
$('#circle').on('mouseover', function() {
$('#line').show();
});
$('#circle').on('mouseleave', function() {
$('#line').hide();
});
jQuery would be simplest.
$('#circle').hover(function(){
$('#line').css('display','inline');
});
or whatever display property you are going for.
Try this in script tag and use onmouseover and onmouseout events
<script>
function hidediv() {
document.getElementById('line').style.display = 'none';
}
function showdiv() {
document.getElementById('line').style.display = 'block';
}
</script>
<div id='circle' onmouseover="hidediv();" onmouseout="showdiv();" > this is circle
<div id= 'line'>this is line
</div>
</div>
I have 2 divs that are initially hidden
<div id="whistle" style="display:none;">
<div id="lean" style="display:none;">
I also have a div that is visible
<div id="me" style="display:block">
I have jQuery code that allows only the #whistle or #lean divs to be open at once, their buttons will hide the other.
I currently have code that also hides the #me div, but I would now like the #me div to open back up when both #whistle and #lean are closed.
If you want to see the site, the link is maxdev.tk
The jQuery code is
$(document).ready(function(){
$("#calc").click(function(){
$("#whistle").hide(600);
$("#lean").toggle(900);
});
});
$(document).ready(function(){
$("#whi").click(function(){
$("#lean").hide(600);
$("#whistle").toggle(900);
});
});
This is one way to solve it. Find it also as a pen at the end of this post.
$(document).ready(function() {
function callback() {
if( $('#whistle').hasClass('hidden') && $('#lean').hasClass('hidden') ) {
$('#me').removeClass('hidden');
} else {
$('#me').addClass('hidden');
}
}
$('button[data-for=whistle]').on('click', function() {
$('#whistle').toggleClass('hidden');
$('#lean').addClass('hidden');
callback();
});
$('button[data-for=lean]').on('click', function() {
$('#lean').toggleClass('hidden');
$('#whistle').addClass('hidden');
callback();
});
})
.hidden {
opacity: 0;
height: 0;
overflow: hidden;
padding: 0;
}
div {
background-color: #ccc;
border-radius: 25px;
margin-top: 20px;
padding: 50px;
text-align: center;
transition-duration: 0.4s;
width: 50%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button data-for="whistle">Whistle</button>
<button data-for="lean">Lean</button>
<div id="whistle" class="hidden">Whistle!</div>
<div id="lean" class="hidden">Lean!</div>
<div id="me">Me!</div>
http://codepen.io/anon/pen/yNJrwe
Add this code to the end of whatever buttons' click function.
if( !$('#whistle').is(':visible') && !$('#lean').is(':visible') ) {
$('#me').css("display","block"); // or use .show();
} else {
$('#me').css("display","none"); // or use .hide();
}
I have tow toggles. I want appear only one toggle at the time. When i click to second toggle then first toggle should be close.
Javascript
$('#bar').click(function () {
$('#foo').slideToggle('slow');
});
$('#bar1').click(function () {
$('#foo1').slideToggle('slow');
});
HTML
<button id="bar">bar</button>
<div id="foo"></div>
<button id="bar1">bar1</button>
<div id="foo1"></div>
CSS
#foo {
width: 100px;
height: 100px;
background-color: green;
display:none;
}
#foo1 {
width: 100px;
height: 100px;
background-color: green;
display:none;
}
jsfiddle
You can use classes instead of id's
$('.bar').click(function () {
$('.foo').hide(); // hide previous elements
$(this).next().show('slow'); // show next element in the DOM (it will be <div> with class 'foo')
});
Example
I did what you want with classes,
the accordion style,
$('#bar, #bar1').click(function () {
var id = '#'+$(this).attr('data-for');
if ($(id).hasClass('open')) {
$(id).toggleClass('open');
}
else if ($('#foo').hasClass('open') || $('#foo1').hasClass('open')) {
$('#foo').toggleClass('open');
$('#foo1').toggleClass('open');
}
else {
$(id).toggleClass('open');
}
});
#foo {
width: 100px;
height: 0;
background-color: green;
display:block;
transition: all .5s;
}
#foo1 {
width: 100px;
height: 0;
background-color: green;
display:block;
transition: all .5s;
}
#foo.open, #foo1.open {
height: 100px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="bar" data-for="foo">bar</button>
<div id="foo"></div>
<button id="bar1" data-for="foo1">bar1</button>
<div id="foo1"></div>
hi i have two ways which you can achive it
in this case the first div is sliding up when second div is opening
$('#bar').click(function () {
$("div").slideUp("slow");
$('#foo').slideToggle('slow');
});
$('#bar1').click(function () {
$("div").slideUp("slow");
$('#foo1').slideToggle('slow');
});
case 1 in fiddler
in second case am hiding the first div when am opening the second div
$('#bar').click(function () {
$("div").hide();
$('#foo').slideToggle('slow');
});
$('#bar1').click(function () {
$("div").hide();
$('#foo1').slideToggle('slow');
});
case 2 in fiddler
i hope my answer helps you :)