$('.tabbed-block .tab-content:first').show();
$('.tabbed-block ol li:first').addClass('active');
$('.tabbed-block ol li a').click(function () {
$('.tabbed-block ol li').removeClass('active');
$(this).parent().addClass('active');
var a = $(this).attr('href');
$('.tabbed-block .tab-content').hide();
$(a).show();
return false;
});
.. works great, but not if used more than once of the same page, interferes with each other. What should I change?
Thanks!
This should work. Although, why aren't you just using jQuery UI Tabs?
$('.tabbed-block').each(function(){
var $block = $(this);
$block.find('.tab-content:first').show();
$block.find('ol li:first').addClass('active');
$block.find('ol li a').click(function () {
var $link = $(this);
$link.closest('ol').find('li a').removeClass('active');
$link.parent().addClass('active');
var a = $link.attr('href');
$link.closest('.tabbed-block').find('.tab-content').hide();
$(a).show();
return false;
});
});
Related
I am using ajax to load the page. But when my page load I have jQuery on the loaded page template that is not working . If i refresh the page then it start working.
I am using ready function as:
jQuery(document).ready(function() {
jQuery('#tabs li a:not(:first)').addClass('inactive');
jQuery('.tab-content').hide();
jQuery('.tab-content:first').show();
jQuery('#tabs li a').click(function() {
var t = jQuery(this).attr('id');
if (jQuery(this).hasClass('inactive')) {
jQuery('#tabs li a').addClass('inactive');
jQuery(this).removeClass('inactive');
jQuery('.tab-content').hide();
jQuery('#' + t + 'C').fadeIn('slow');
}
});
});
But it not showing any alert when i click on the page link and page is loaded using ajax without refresh.
How i can make the jquery workable please help
I have to load two function at the success of the ajax call only one of them is wokring not both of them at the same time:
$.ajax({
url: url,
success:
function(data){
$('#tabs li a:not(:first)').addClass('inactive');
$('.tab-content').hide();
$('.tab-content:first').show();
$('#tabs li a').click(function(){
var t = $(this).attr('id');
if($(this).hasClass('inactive')){
$('#tabs li a').addClass('inactive');
$(this).removeClass('inactive');
$('.tab-content').hide();
$('#'+ t + 'C').fadeIn('slow');
}
});
}
function(data, textStatus, jqXHR){
alert("working");
},
error: function(jqXHR, textStatus, errorThrown){
document.location.href = url;
return false;
}
How it could be possible?
Please use the success function to find whether the functionality is executing successfully or not,
$(document).ready(function (){
$.ajax({
jQuery('#tabs li a:not(:first)').addClass('inactive');
jQuery('.tab-content').hide();
jQuery('.tab-content:first').show();
jQuery('#tabs li a').click(function() {
var t = jQuery(this).attr('id');
if (jQuery(this).hasClass('inactive')) {
jQuery('#tabs li a').addClass('inactive');
jQuery(this).removeClass('inactive');
jQuery('.tab-content').hide();
jQuery('#' + t + 'C').fadeIn('slow');
}
});
success: function(this) {
alert(this);
}
});
});
I got is i have laoded my Jquery in a new function on ajaxSuccess as follows and it works perfectly
jQuery(document).ready(function(){
jQuery(document).ajaxSuccess(function() {
jQuery('#tabs li a:not(:first)').addClass('inactive');
jQuery('.tab-content').hide();
jQuery('.tab-content:first').show();
jQuery('#tabs li a').click(function(){
var t = jQuery(this).attr('id');
if(jQuery(this).hasClass('inactive')){
jQuery('#tabs li a').addClass('inactive');
jQuery(this).removeClass('inactive');
jQuery('.tab-content').hide();
jQuery('#'+ t + 'C').fadeIn('slow');
}
});
});
I'm trying to make a sidebar that when clicked it (aside li) slides down the section paragraph (section p) with the same class.
var $asideGet = $('aside li').click(function(){
$(this).each(function(){
$(this).attr('class');
});
});
var $sectionGet = $('section p').click(function(){
$(this).each(function(){
$(this).attr('class');
});
});
if($asideGet == $sectionGet){
$asideGet.click(function(){
$sectionGet.slideToggle();
});
};
I reviwed the code and got something like this:
Oops... But when I click the aside li, it slides down all the section p, and I just want to slide down the section p with the same class as aside li does.
var $sectionP = $('section p');
$sectionP.hide();
var $asideLi = $('aside li');
function comparing(){
$asideLi.click(function(){
$(this).each(function(){
var $asideClass = $(this).attr('class');
if($sectionP.hasClass($asideClass)){
$sectionP.slideToggle();
};
}); //each function
}); //click function
};
JSFiddle
http://jsfiddle.net/5rkrq4bw/strong text
JQuery Code
// Side Menu Starts
$('.SideNav .Menu a.MenuDrop').click(function(event){
event.preventDefault();
if(!$(this).hasClass('Active')) {
if(!$(this).parent().parent().hasClass('Active') && $(this).next().hasClass('sub-menu')) {
$(this).next().slideToggle();
$(this).addClass('Active');
} else {
$('.SideNav .Menu li ul').slideUp();
$(this).next().slideToggle();
$('.SideNav .Menu a.MenuDrop').removeClass('Active');
$(this).addClass('Active');
}
}
});
//Side Menu Ends
The Problem
Trying to integrate multiple tiers I am finding the problem of only being able to have one open at a time and checking to see if any others are open to close them.
What should happen
Demo
Category
Sub-Cat
Link
Link
Link
Sub-Cat
Link
Link
Link
Category
Sub-Cat
Link
Link
Link
Sub-Cat
Link
Link
Link
Explanation
Only one 'Category' to be expanded at a time
Only one 'Sub-Cat' inside to be expanded at a time
Adding / removing of 'Active' class.
Resolved: http://jsfiddle.net/wo4sj4pt/
JQuery Code:
(function(jQuery){
jQuery.fn.extend({
accordion: function() {
return this.each(function() {
var $ul = $(this);
if($ul.data('accordiated'))
return false;
$.each($ul.find('ul'), function(){
$(this).data('accordiated', true);
$(this).hide();
});
$.each($ul.find('a'), function(){
$(this).click(function(e){
activate(this);
return void(0);
});
});
var active = $('.Active');
if(active){
activate(active, 'toggle');
$(active).parents().show();
}
function activate(el,effect){
if (!effect) {
$(el)
.toggleClass('active')
.parent('li')
.siblings()
.find('a')
.removeClass('active')
.parent('li')
.children('ul, div')
.slideUp('fast');
}
$(el)
.siblings('ul, div')[(effect || 'slideToggle')]((!effect)?'fast':null);
}
});
}
});
})(jQuery);
Usage:
Link to JQuery and the above script
Make a multi-level list
Give your list a class/ID name such as '.SideNav'
Tell the script this is your accordion $('.SideNav').accordion();
http://jsfiddle.net/sabithpocker/5rkrq4bw/2/
$('.SideNav .Menu a.MenuDrop').click(function(event){
event.preventDefault();
console.log(this);
var subMenuToExpand = $('ul.sub-menu', $(this).parent());
var otherVisibleSubMenu = $('ul.sub-menu:visible', $(this).parents('.Menu'));
otherVisibleSubMenu.hide();
subMenuToExpand.show();
});
$('.SideNav .Menu a.MenuDrop').click(function(event){
$('ul.sub-menu:visible', $(this).parents('.Menu')).slideUp(50);
$('ul.sub-menu', $(this).parent()).slideDown();
});
I have this jQuery code that opens an accordeon on hover but i need to make it work each tab on click instead, i've tried to change the "hover" to "click" but no success, could someone here please help me ?
Thanks in advance.
$(function() {
$('#accordion > li').hover(function() {
var $this = $(this);
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
}, function() {
var $this = $(this);
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
});
});
the idea from Tushar Gupta is the only one that's partially working, it opens the accordeon on click, but if the user clicks another tab while one is open there's a bug...
i make a fiddle with the whole code.
http://jsfiddle.net/C8Kp8/ <-- Tushar Gupta's solution
http://jsfiddle.net/SHmuc/ <-- Original Code
thank you all for your help, its really appreciated.
You can use .toggle() or this
$(function() {
$('#accordion > li').click(function() {
var $this = $(this);
if ($this.hasClass('open')) {
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
$this.removeClass('open');
}
else {
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
$this.addClass('open');
}
});
});
Take a look at this. #Alex's answer is good but ignores the first click, and doesn't close the open elements when a closed element is clicked. FIDDLE. In this version, the more links in the accordion elements also work.
$(function() {
$('#accordion li').click(function() {
var $this = $(this);
if (!$this.hasClass('open')) {
// open clicked accordion element
$this.stop().animate({'width':'480px'},500);
$('.heading',$this).stop(true,true).fadeOut();
$('.bgDescription',$this).stop(true,true).slideDown(500);
$('.description',$this).stop(true,true).fadeIn();
// close other open accordion element
$("#accordion li.open").stop().animate({'width':'115px'},1000);
$("#accordion li.open .heading").stop(true, true).fadeIn();
$("#accordion li.open .description, #accordion li.open .bgDescription").stop(true, true).fadeOut();
$("#accordion li.open").removeClass("open");
$this.addClass('open');
}
else {
// close this accordion element
$this.stop().animate({'width':'115px'},1000);
$('.heading',$this).stop(true,true).fadeIn();
$('.description',$this).stop(true,true).fadeOut(500);
$('.bgDescription',$this).stop(true,true).slideUp(700);
$this.removeClass('open');
}
});
$('#accordion > li a').click(function(e){
e.stopPropagation();
});
});
I want to add a class to the selected 'li' and at the same time, remove the class:selected from previous selected li element.
I have worked on it hours and still haven't got any luck. I also checked others questions, but their solutions don't work for me.
Help please....
<ul id='mainView' class='menu' style='float: left; clear: both;'>
<li>Patient</li>
<li>Recommendations</li>
</ul>
<script type="text/javascript">
$(document).ready(function () {
$('.menu ul a').on('click', function (event) {
$('.menu ul a.selected').className = '';
alert($(this).attr('id'));
$(this).attr('class') = 'selected';
});
});
// $('.menu li').on('click', function () {
// $('.menu li.selected').className = '';
// this.className = 'selected';
// });
</script>
Update:
I did put a inside li, but if I click on the li not the a inside of the li, the webpage does not redirect. That's the reason why I do it in a reversed way.
Update 2
The reason why the selected li does not get the "selected" class is because the whole webpage is redirected to a new page which has the same navigation bar.
So now the question is how to highlight the selected li(it was selected on the previous page) on the new webpage.
Inside an UL everybody (even a browser) is expecting to see a LI
so your HTML:
<ul>
<li>Patient</li>
<li>Recommendations</li>
</ul>
And your jQ:
$('ul li').click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
Building web pages you should know how to treat LI elements. Simple, like dummy containers with minimal styling.
That means that you rather add a display:block ... float:left and other cool stuff to the <A> elements, than setting a padding there you go with your full-sized-clickable A elements.
Additionally (if you don't have time to play with CSS) to make a LI fully clickable use:
$('ul li').click(function(){
var goTo = $(this).find('a').attr('href');
window.location = goTo ;
// $(this).addClass('selected').siblings().removeClass('selected'); // than you don't need this :D
});
After the OP late edit - and to answer the question
After the pages refreshes to get which one is the active one use:
// ABSOLUTE PATH
var currentPage = window.location;
// RELATIVE PATH
// var currentPage = window.location.pathname;
$('li a[href="'+ currentPage +'"]').addClass('selected');
<script type="text/javascript">
$(document).ready(function () {
$('.menu ul a').on('click', function (event) {
$('.menu ul a.selected').removeClass('selected');
alert($(this).attr('id'));
$(this).addClass('selected')
});
});
</script>
Try addClass and removeClass, they're jQuery functions:
$(document).ready(function () {
$('.menu ul a').on('click', function (event) {
$('.menu ul a.selected').removeClass("selected");
$(this).addClass('selected');
});
});