I got a question regarding the toggle functionality of jQuery. I want to achieve that if I click on the arrow the content becomes visible and all other contents are hidden.
I use this JS code to toggle the class within a generic way:
$(function () {
$('.main-diff .blue-bg .arc_arrow').click(function () {
$(this).toggleClass('arc_arrow--displayed');
$(this).parent().next('.matchfooter').toggleClass('matchfooter--displayed');
});
});
To look at my HTML code and CSS you could have a look here: JSFIDDLE
I tried to add $('.matchfooter').removeClass('matchfooter--displayed'); but that does not hide the content anymore if I click again on the same arrow.
You need to remove class from other elements, which can be achieved using .not() selector.
$('.main-diff .blue-bg .arc_arrow').click(function () {
//Hide others
$('.main-diff .blue-bg .arc_arrow').not(this).removeClass('arc_arrow--displayed');
$('.main-diff .blue-bg .arc_arrow').not(this).parent().next('.matchfooter').removeClass('matchfooter--displayed');
$(this).toggleClass('arc_arrow--displayed');
$(this).parent().next('.matchfooter').toggleClass('matchfooter--displayed');
});
DEMO
Instead of toggle you can just add class and remove it on clicking others.
$(function () {
$('.main-diff .blue-bg .arc_arrow').click(function () {
//Hide others
$('.main-diff .blue-bg .arc_arrow').not(this).removeClass('arc_arrow--displayed');
$('.main-diff .blue-bg .arc_arrow').not(this).parent().next('.matchfooter').removeClass('matchfooter--displayed');
$(this).addClass('arc_arrow--displayed');
$(this).parent().next('.matchfooter').addClass('matchfooter--displayed');
});
});
Fiddle
I have solved your issue, just change the jquery code with following and try again :-
$(function () {
$('.main-diff .blue-bg .arc_arrow').click(function () {
$(this).toggleClass('arc_arrow--displayed');
$(".matchfooter").removeClass("matchfooter--displayed");
$(this).parent().next('.matchfooter').toggleClass('matchfooter--displayed');
});
});
Related
I am trying to hide a div whenever I will hover over it and show another one in the same place.. And when I take the mouse out of that.. the previous div will be shown and this div will be hidden...
$(document).ready(function(){
$('#hover_tutor').hover(
function () {
$('#hover_tutor').hide();
$('#hover_tutor_hidden').show();
},
function () {
$('#hover_tutor').show();
$('#hover_tutor_hidden').hide();
}
);
});
<div id="hover_tutor">Blah</div>
<div id="hover_tutor_hidden" style="display:none;">Bleh</div>
But on hovering the hover_tutor... something is happening.. It's jumping up and down.. I don't know what's wrong...
You need to use .mouseenter event for #hover_tutor div and .mouseleave for #hover_tutor_hidden div:
$('#hover_tutor').mouseenter(function () {
$(this).hide();
$('#hover_tutor_hidden').show();
});
$('#hover_tutor_hidden').mouseleave(function () {
$('#hover_tutor').show();
$(this).hide();
}
).mouseleave();//trigger mouseleave to hide second div in beginning
Working Demo
You can also use toggle method instent of hide/show on hover event
<div id="hover_tutor" style="display: block;">Blah</div>
<div id="hover_tutor_hidden" style="display: none;">Bleh</div>
$('#hover_tutor').hover(
function () {
$('#hover_tutor').toggle();
$('#hover_tutor_hidden').toggle();
});
Working demo
http://jsfiddle.net/ivyansh9897/8jgjvkqk/
try this,
$('#hover_tutor').hover(function () {
$(this).hide();
$('#hover_tutor_hidden').show();
});
$('#hover_tutor_hidden').hover(function () {
$('#hover_tutor').show();
$(this).hide();
}
));
If you do have the flexibility to modify your html little bit using class attribute there's a better way. Use .toggle() to alter the state of your element on both mouseover and mouseleave.
HTML :
<div id="hover_tutor" class="hello">Blah</div>
<div id="hover_tutor_hidden" class="hello" style="display:none;">Bleh</div>
jQuery :
$(".hello").on("mouseover mouseleave", function(){
$("#hover_tutor").toggle();
$("#hover_tutor_hidden").toggle();
});
jsFiddle
I am currently implementing a set of tabs similar to the style of an accordian.
This is my code so far...
http://codepen.io/anon/pen/uqBnH
I am having trouble closing the others when one is selected.
My jQuery code...
$(function(){
$('.panel').hide();
$('.mobtabs').click(function(){
$(this).next().toggle();
});
});
Any help would be greatly appreciated.
Just toggle the others shut with:
$(function () {
$('.panel').hide();
$('.mobtabs').click(function () {
$('.mobtabs').not($(this)).next().hide();
$(this).next().toggle();
});
});
jsFiddle example
You can use:
$(function () {
$('.panel').hide();
$('.mobtabs').click(function () {
var nextPanel = $(this).next();
$('.panel').not(nextPanel).hide();
nextPanel.toggle();
});
});
Fiddle Demo
Make the following changes (I added the sliding) -
$('.panel').hide();
$('.mobtabs').click(function(){
$('.mobtabs').not( $(this) ).next().slideUp(); // close them all
$(this).next().slideToggle(); // slide open (or closed if it applies) this tab
});
I'm creating a new site and I want to add a button that hides certain sections of the site when clicked. Right now I'm using the code below and it works fine but I just need it to do one more thing. How can I make it add a new class to the button when it's clicked so I can reposition the clicked button using css? Thanks in advance!
Here is the js code i'm using right now.
$(".hide-content").click(function() {
$(".site-header, #page-entry, .site-footer").toggle(500);
});
$(".hide-content").toggle(function() {
$(this).text("Hide");
}, function() {
$(this).text("Show");
});
});
Try .addClass()
$(".hide-content").click(function() {
$(".site-header, #page-entry, .site-footer").toggle(500);
$(this).addClass('ClassName'); //add class
});
Also Read Class Attribute
.toggleClass()
.removeClass()
this
.toggle() has been deprecated and removed
$(".hide-content").click(function () {
$(".hide-content").addClass("new-class")
$(".site-header, #page-entry, .site-footer").toggle(500);
});
$("body").on('click', '.hide-content', function (e) {
var $el = $(this);
$(".site-header, #page-entry, .site-footer").toggle(500, function () { //when complete..
$el.addClass('myclass');
});
});
Use the addClass function
$(".hide-content").click(function() {
$(this).addClass("class-name")
$(".site-header, #page-entry, .site-footer").toggle(500);
});
Or you could do css directly on jquery using the .css function
$(".hide-content").click(function() {
$(this).css("position", "absolute"); //note that params in css function is only an example, do what you need..
$(".site-header, #page-entry, .site-footer").toggle(500);
});
I have this code in html:
<div class="sub-status">
<p class="subscribed"><i class="icon-check"></i> Subscribed</p>
</div>
On hover, I want that to be changed to:
<div class="sub-status">
<p class="unsubscribe"><i>X</i> Unsubscribe</p>
</div>
And, I have this code in jQuery:
$(document).ready(function() {
$('.sub-status').mouseenter(function() {
$(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
});
$('.sub-status').mouseleave(function() {
$('this').html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
});
});
The first function is working great. When I mouseover that div, it is changed to what I want, but the mouseleave is not working. I want that when I put my mouse out of that div, its data will return to like it was before. I can't get this working. Any help would be appreciated.
Thanks.
Change
$('this')...
to
$(this)...
And you can use hover() instead of using two separate functions:
$('.sub-status').hover(function() {
$(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
},function() {
$(this).html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
});
Updated
Your fiddle isn't working since you are updating the entire content of the hovered element - update just the text in <p> should work.
$('.sub-status').hover(function() {
$(this).children('p')
.removeClass()
.addClass('unsubscribed')
.html("<i>X</i> Unsubscribe");
},function() {
$(this).children('p')
.removeClass()
.addClass('subscribed')
.html("<i class='icon-check'></i> Subscribed");
});
Working fiddle
Here, try this. Working demo: http://jsfiddle.net/XrYj4/3/
$(document).ready(function() {
$('.sub-status').on("mouseenter", function() {
$(this).find("p").prop("class", "unsubscribed").html("<i>X</i> Unsubscribe");
}).on("mouseleave", function() {
$(this).find("p").prop("class", "subscribed").html("<i class='icon-check'></i> Subscribed");
});
});
Try to use a hover function:
$(".sub-status").hover(
function () {
$(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
},
function () {
$(this).html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
}
);
http://api.jquery.com/hover/
Change 'this' to simply this. Also consider chaining, shown below, this helps users with weaker devices load stuff faster.
$(document).ready(function() {
$('.sub-status').mouseenter(function() {
$(this).html("<p class='unsubscribe'><i>X</i> Unsubscribe</p>");
}).mouseleave(function() {
$(this).html("<p class='subscribed'><i class='icon-check'></i> Subscribed</p>");
});
});
I have this script which needs to work on an ipad. It was working on chrome with live, however, moving it to on makes it unresponsive.
Any ideas, I would be grateful!
$("#clickAll").on("click", function () {
$(".welcome1poi").show();
$(this).addClass("active");
});
$("#clickAll.active").on("click", function () {
$(this).removeClass("active");
$(".welcome1poi").hide();
});
try this
$("#clickAll").on("click", function(){
$(".welcome1poi").toggle();
$(this).toggleClass("active");
});
updated
as suggested
$(document).on('click',"#clickAll", function(){
$(".welcome1poi").toggle();
$(this).toggleClass("active");
});
since it was working with live() i assume your element with id clickAll is added dynamically so try this
$(document).on("click","#clickAll", function () {
$(".welcome1poi").show();
$(this).addClass("active");
});
$(document).on("click","#clickAll.active", function () {
$(this).removeClass("active");
$(".welcome1poi").hide();
});
you can replace the $(document) selector with your closest element to #clickAll element which will be more efficient
This will delegate the event to the body and it will be caught when it bubbles up the DOM.
$('body').on("click", "#clickAll", function(){
$(".welcome1poi").show();
$(this).addClass("active");
});
$('body').on("click", "#clickAll.active", function(){
$(this).removeClass("active");
$(".welcome1poi").hide();
});
Are you adding and removing the "active" class in order to create a toggle effect? If so, try .toggle(), like so:
$('#clickAll).toggle(
function() { $('.welcome1poi').show(); },
function() { $('.welcome1poi').hide(); });