DEMO
What Iam trying to Achieve...
If the user clicks on 1st list item, - background is added
If the user AGAIN Clicks on same List Item - background should be removed
which is not happening. can someone plz look into this.. i tried toggleClass as well, but its not working..
Thanks!!
ageUrl = "";
$('.ageUrl li a').on('click', function(e){
e.preventDefault();
ageUrl = $(this).attr("href");
if($(this).hasClass('selectedBg')){
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg');
}
else {
//$(this).addClass('selectedBg');
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg');
$(this).closest('li').addClass('selectedBg');
//$(this).closest('li').toggleClass('selectedBg')
console.log(ageUrl);
}
});
$('.crossIconSel').on('click', function(){
alert('hi');
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg, crossIconSel');
});
Demo Fiddle Try this one
$('.ageUrl li a').on('click', function(e){
e.preventDefault();
ageUrl = $(this).attr("href");
if($(this).parent('li').hasClass('selectedBg')){
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg');
}
else {
//$(this).addClass('selectedBg');
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg');
$(this).closest('li').addClass('selectedBg');
//$(this).closest('li').toggleClass('selectedBg')
console.log(ageUrl);
}
});
$('.crossIconSel').on('click', function(){
alert('hi');
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg, crossIconSel');
});
Your if condition was wrong as you were checking on a element that it has class SelectedBg whereas you have applied the class on .closest(li):
Just make the if condition like this:
if ($(this).closest('li').hasClass('selectedBg')) {
$('.ageUrl li, .ageUrl li a').removeClass('selectedBg');
}
Working Demo
See if this works
Jquery
$('.ageUrl li a').on('click', function (e) {
e.preventDefault();
$('.ageUrl li a').not(this).removeClass('selectedBg');
if( $(this).hasClass('selectedBg')){
$(this).removeClass('selectedBg');
}
else{
$(this).addClass('selectedBg');
}
});
Working Code ->JSFIDDLE
Related
I have a simple ol li based list which toggles when i click on parent item or child item. I want it to toggle only when parent item is clicked.
$("#expList > li").click(function () {
$(this).find("ol").slideToggle();
});
https://jsfiddle.net/k2jqqbbk/3/
I tried to target it with $("#expList ol li") but this is not working. I tried few other options which didn't work either.
You could use the event.target to check whether its closest li contains an ol, and only toggle if it does:
$('#expList > li').click(function(evt) {
if ($(evt.target).closest('li').has('ol').length) {
$(this).find("ol").slideToggle();
}
});
Here's a fiddle
You need to target the a tag. Because the ol is the next child, the find call has been replaced with a call to next:
$("#expList > li > a").click(function () {
$(this).next().slideToggle();
});
If you want the functionality to extend to children of children, just omit #expList from the selector:
$("li > a").click(function () {
$(this).next().slideToggle();
});
Fiddle
Try to add the following code :
$("#expList > li").click(function () {
$(this).find("ol").slideToggle();
});
$("#expList > li ol li").click(function () {
return false;
});
The click event bubbles up to ancestor elements. So the click is fired first against the second-level list items, then bubbles up to the first-level list items. This is why you're seeing the toggling happen.
All you need to do is stop the click event bubbling up the element chain from the second-level list items.
Add this to fix:
$("#expList ol li").click(function(e) {
e.stopPropagation();
});
Here's the example: https://jsfiddle.net/sxn5bg3x/
When I hover on .menu li all of a take this class.
I want to hover on .menu li so that just his child a takes the class.
$(document).ready(function() {
// Menu is hover
$('.menu li').hover(function(){
$(this).addClass('liHoverMnue');
$('.menu li a').addClass('aHoverMnue');
}, function() {
$(this).removeClass('liHoverMnue');
$('.menu li a').removeClass('aHoverMnue');
});
});
Use the this keyword again
$(document).ready(function() {
// Menu is hover
$('.menu li').hover(function(){
$(this).addClass('liHoverMnue');
$(this).find('a').addClass('aHoverMnue');
}, function() {
$(this).removeClass('liHoverMnue');
$(this).find('a').removeClass('aHoverMnue');
});
});
You could also chain it
$(this).addClass('liHoverMnue').find('a').addClass('aHoverMnue');
So I have this code from a previous question: http://jsfiddle.net/928Dj/4/
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function(e) {
$(this).next('ul').toggle();
});
I was wondering if anyone could tell me how I allow it to still be toggled open and closed, but also make it so if one menu is open and I click to open another menu, it closes the first one, meaning only one menu can be open at any time.
Try to hide the opened ul elements first and then toggle the current element,
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function (e) {
var cache = $(this).next('ul');
$('#filter ul:visible').not(cache).hide();
cache.toggle();
});
DEMO
You can hide all the visible submenus before showing the current one as follows
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function(e) {
var ul = $(this).next('ul');
$('ul li ul:visible').not(ul).hide();
ul.toggle();
});
Demo
This is the best way. You can also try this:
$("ul.opt").addClass("hidden");
$('#filter > li > a').on("click", function(e) {
$(this).next('ul').toggle();
$(this).parent().siblings().find('.opt').hide();
});
Fiddle
I made an click event for .menu li which has ul in it using $(".menu li:has(ul)"). It works but it also assigns the same event to .menu li ul li. I tried using e.stopPropagation();, e.PreventDefault(); and return false; inside function but that didn't worked at all. How to prevent that?
Here's the fiddle to show up actual problem.
jQuery I used:
$(".menu li:has(ul)").click(function() {
console.log('has ul');
if($(this).children("ul").is(':visible')){
$(this).children("ul").slideUp();
}else{
$(this).children("ul").slideDown();
}
});
Try this instead:
$('.menu a').click(function() {
var next=$(this).next();
if(next.prop('tagName')=='UL') {
if(next.is(':visible')){
next.slideUp();
}else{
next.slideDown();
}
}
});
It's Works now Fiddle you need to add return false on child UL
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');
});
});