I have 3 buttons which when clicked collapses and a form is shown. What I want is if one is already collapsed then I want it to hide again if another one is clicked.
My fiddle: https://jsfiddle.net/77soggnv/
This is my js code:
$(document).ready(function(){
//See which panel has been clicked
$("#citizen").click(function(){
$(this).data('clicked', true);
});
$("#organisation").click(function(){
$(this).data('clicked', true);
});
$("#anonymous").click(function(){
$(this).data('clicked', true);
});
//Hide the other panels if one is clicked
if($("#citizen").data('clicked')){
$("#organisation").collapse("hide");
$("#anonymous").collapse("hide");
}
if($("#organisation").data('clicked')){
$("#citizen").collapse("hide");
$("#anonymous").collapse("hide");
}
if($("#anonymous").data('clicked')){
$("#organisation").collapse("hide");
$("#citizen").collapse("hide");
}
});
Simply you can do it like:
$(".btn").click(function(){
$('.collapse.in').collapse('hide');
});
Updated Fiddle
Since the clicking elements have common classnames and structure, we don't have to attach event separately.
$(".btn.btn-primary").click(function() {
var next = $(this).next();
$(".collapse").not(next).slideUp();
next.slideToggle();
});
Fiddle
next() method will return the element after the current element.
Related
I've got the following list of semibuttons loaded using javascript:
var html ='<ul class="nav well-tabs well-tabs-inverse mb10" id="users">';
html +='<li class="active"><a id="'+this.my.user+'" data-toggle="tab_'+self.my.id+'" class="pestaƱa">'+this.my.user+'</a></li>';
var users = this.my.community_users;
for (i=0;i<users.length;i++) {
if (users[i].user != this.my.user)
html +='<li><a id="'+users[i].user+'" data-toggle="tab_'+self.my.id+'" class="pestana">'+users[i].user+'</a></li>';
};
html +='</ul>';
$(html).appendTo("#Dashboard");
Note, that the first item in the list is active. I am getting something like this:
Ok, now i code he onclick event to do something when a button is clicked:
$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function(e){
// whatever here
});
What I need now is to set active the tab being clicked and set inactive the tab that was active. How can I access both elements to addclass and removeclass active?
You could use following logic:
$(document).on('click', '#users li:not(.active)', function () {
$('#users').find('li.active').add(this).toggleClass('active');
});
Something like this might work. Basically remove the .active class from everything but the element you clicked on. The add the .active class to the element clicked on.
$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function (e) {
$('a[data-toggle=tab_'+self.my.id+']').not(this).removeClass('active');
$(this).addClass('active');
});
I would remove the 'active' class from all the list items first, then add it back to just the only that was clicked.
$(document).on('click', 'a[data-toggle=tab_'+self.my.id+']', function (e) {
$('#users .active').removeClass('active');
$(this).addClass('active');
});
I have multiple div with same class.
When a user taps on one div i have a slidetoggle() in that i have two buttons "accept" and "reject". Now when the user clicks on the accept or rejects i want that specific div to change colour to green or red based on accept or reject.
But when i try to assign the colour all the div change colour.
Is there anyway to change the colour of the specific div.
$(document).on("pagecreate","#one1",function(){
$("div.comp2").on("tap",function(){
$("#panel").slideToggle("slow", function(){
$("#accept").on("click",function(){
$("div.comp2").css("background-color","#22bb45");
});
});
});
});
Within the event handler you can use the this keyword to refer only to the element which raised the event, instead of selecting all of the elements by class. Try this:
$(document).on("pagecreate","#one1",function(){
$("div.comp2").on("tap", function() {
var $comp2 = $(this);
$("#panel").slideToggle("slow", function(){
$("#accept").on("click", function(){
$comp2.css("background-color","#22bb45");
});
});
});
});
$(document).on("pagecreate","#one1",function(){
$("div.comp2").on("tap",function(){
var __this = this;
$("#panel").slideToggle("slow", function(){
$("#accept").on("click",function(){
$(__this).css("background-color","#22bb45");
});
});
});
});
I'm using stopPropagation to make a parent menu item that links to /# to show a dropdown child menu when clicked. It does show the child menu when clicked, but it first goes to url/# before showing the child menu. I'd like it to ignore its link and just show the dropdown menu. My code is below:
$(document).on('click', '#navpanel .mainnav a[href^="/#"]', function(e){
if($(this).parent('li').hasClass('expanded')) {
$(this).siblings('ul').css('display', 'block').slideDown('linear', function(){
$(this).parent('li').toggleClass('expanded');
});
} else {
$(this).siblings('ul').css('display', 'none').slideUp('linear', function(){
$(this).parent('li').toggleClass('expanded');
});
}
var objHeight = 0;
$.each($('ul.tier1').children(), function(){
objHeight += $(this).height();
});
$('ul.tier1').height(objHeight);
e.stopPropagation();
});
If i understood you right you need not stopPropagation but prevent default. Add this to your link click event. It will disable default action, which in your case is redirect.
e.preventDefault();
stopPropagation just prevents your event from bubling in the DOM tree
So I need a little bit of help. I'm playing around with addClass and removeClass and I can't seem to remove a class after it's set. What I basically want is:
When someone clicks an h3, it adds to its parent div class
When someone clicks a div with added class, class needs to be removed
First step I got out of way and it's working
$(function(){
$('div h3.itemTitle').on('click', function(){
$(this).parent().addClass('active').siblings().removeClass('active');
});
});
Now when I define:
$(function(){
$('div.active').on('click', function(){
$(this).removeClass('active');
});
});
It does nothing, as if it doesn't see classes. It sets only those set in onload...
Help, anyone?
The child element "h3.itemTitle" already had a click event listener on it and the parent can't actually capture the click event.
Your $('div.active').on('click', ...) never actually fires because you click the h3 not the div.
I recommend this approach: http://jsfiddle.net/c3Q6Q/
$('div h3.itemTitle').on('click', function () {
// saves time not to write $(this).parent() everything so i store in a _parent var
var _parent = $(this).parent();
if (_parent.hasClass('active')) {
_parent.removeClass('active');
} else {
_parent.addClass('active').siblings().removeClass('active');
}
});
Try
$('body').on('click','div.active', function(){$(this).removeClass('active');});
Instead of
$('div.active').on('click', function(){$(this).removeClass('active');});
I would go with this way:
$('div').on('click', function(e){
var el = e.target;
if($(el).is('h3') && $(el).hasClass('itemTitle')){
$(this).parent().addClass('active').siblings().removeClass('active');
}else if($(el).is('div') && $(el).hasClass('active')){
$(this).removeClass('active');
}
});
Not sure why every is talking about elements generated outside of the initial DOM load.
Here's a JSFiddle showing that it works: http://jsfiddle.net/H25bT/
Code:
$(document).ready(function() {
$('.itemTitle').on('click', function() {
$(this).parent().addClass('active').siblings().removeClass('active');
});
/* $('.parent').on('click', function() {
$(this).removeClass('active');
}); */
$('.clicky').on('click', function() {
$(this).parent().removeClass('active');
});
});
The reason it's not working for you is that if you put the removeClass click event on the parent div itself, clicking on the child text causes a conflict with which click handler to use, and it won't work out. Code works fine if you don't assign the click to the parent div itself.
I have having a little trouble with the slideToggle when I have a link inside of the slideup panel. What I am trying to do is have the ability to press a button and a div will slide up and display related posts and once you press another or the related project button on the page it will close the toggle and reveal another effect that I am using (100% width and heigh popup). The script I am using works perfect but I am running into one problem. When I click a related post inside of the slideToggle it causes the div to slide down instead of going to the page that represents the link.
Here is my code below and an example http://jsfiddle.net/K8vBg/15/.
$(document).ready(function(){
// build a variable to target the #menu div
var menu = $('#menu')
// bind a click function to the menu-trigger
$('#menu-trigger').click(function(event){
event.preventDefault();
event.stopPropagation();
// if the menu is visible slide it up
if (menu.is(":visible"))
{
menu.slideUp(1000);
}
// otherwise, slide the menu down
else
{
menu.slideDown(400);
}
});
$(document).not('.projectHolder-small,#projectSpecs').click(function(event) {
event.preventDefault();
if (menu.is(":visible"))
{
menu.slideUp(400);
}
});
})
If I change .projectHolder-small,#projectSpecs in the .not function to just read #menu then I am able to click the link inside of the panel but the panel will not slideDown when I click another button on the page. The popup from #project specs will just go over the panel instead of closing it.
Is there something I am missing in my script?
Thank you
Try changing the $(document).not().click() to:
$(document).click(function(event){
if(!$(event.target).closest('.projectHolder-small,#projectSpecs').length){
if (menu.is(":visible")){
menu.slideUp(400);
}
}
});
I am using closest() instead of the usual is(), so that even clicking on the children elements of '.projectHolder-small,#projectSpecs' the panel won't close.
I rewrote the script to the following and it works perfect
$(document).ready(function () {
var $frm = $('#menu').hide();
var $bts = $("#menu-trigger").on('click', function () {
var $this = $(this)
$bts.filter(".selected").not(this).removeClass('selected');
$this.toggleClass('selected');
if ($this.hasClass('selected') && $frm.is(':visible')) {
$frm.stop(true, true).slideUp(function () {
$(this).slideDown()
});
} else {
$frm.stop(true, true).slideToggle();
}
});
$bts.filter('.selected').click();
$("#projectSpecs, #menuButton").click(function () {
$bts.filter(".selected").removeClass('selected');
$frm.slideUp();
});
});