0

I have a code in which when I click on particular element its background should change by removing all other elements background.But the problem here I am getting is when I click on li under ul li ul its taking parent li also and giving background. And Please check my code below

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
<script type="text/javascript" src="jquery-1.3.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("ul li ul li").click(function() {  

 //a = $(this).html();         // when clicking any of these links
 //alert(a);
$("*").removeClass("active"); // remove highlight from all links
$(this).addClass("active");          // add highlight to clicked link
})
});
</script>

<script type="text/javascript">
$(document).ready(function(){
$("ul li").click(function() {  

//a = $(this).html();         // when clicking any of these links
//alert(a);
$("*").removeClass("active"); // remove highlight from all links
$(this).addClass("active");          // add highlight to clicked link
})
});
</script>

<style>
.active { color:white; background:green; }
</style>
</head>
<body>
<ul>
<li>level1
        <ul>
        <li>aaa</li>
        <li>bbb</li>
        <li>ccc</li>
        </ul>
</li>
<li>level2</li>
<li>level3</li>
</ul>

</body>
</html>
4
  • 2
    You should provide a fiddle for this kind of thing, makes our lives easier. Did this one for you. Commented Sep 6, 2013 at 7:18
  • 2
    $("*").removeClass("active"); should be replaced with $(".active").removeClass("active"); as it's way more efficient. Commented Sep 6, 2013 at 7:19
  • @jfriend00 changed * to .active but still no changes Commented Sep 6, 2013 at 7:26
  • @user2720197 - I wasn't suggesting my change would fix your problem. I can't understand your question so I have no idea what you problem is. But, what I've suggested is a much better way to remove all existing active classes if that's part of your eventual solution. Commented Sep 6, 2013 at 7:46

4 Answers 4

2

The problem is your event is propagating to the parent, so the code is being executed first in the click handler for the child ul li and then again in the click handler for the parent ul li.

Add a call to event.stopPropagation() in the child's handler:

$(document).ready(function(){
    $("ul li").click(function(evt) {  
        $(".active").removeClass("active"); // remove highlight from all links
        $(this).addClass("active");          // add highlight to clicked link
        evt.stopPropagation();
    })
});

Fiddle


Alternatively, if you need events to propagate elsewhere, but not be handled in this code you can compare event.target to this:

$(document).ready(function(){
    $("ul li").click(function(evt) {  
        if(evt.target !== this) return; //do not process on bubbled-up events

        $(".active").removeClass("active"); // remove highlight from all links
        $(this).addClass("active");          // add highlight to clicked link
    })
});

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

When i click on first ul li its taking all its child li and giving background...but working fine with the child li's..So when i click on parent li only parent element should be highlighted
You mean you want only the line clicked to apply the style? You'll have to manually reset the children's style then. Try adding .active * { color:black; background:white;} to your CSS: jsfiddle.net/TrCqU/6
0

Try it in a simple manner,

$(document).ready(function(){
    $("li").on('click',function() {  
        $(this).parent('ul').find('li').removeClass("active");// get all li from the parent ul
        $(this).addClass("active");// add highlight to clicked link
    });
});

If you want to remove all active classes from all li's then try this,

$(document).ready(function(){
    $("li").on('click',function() {  
        $('li').removeClass("active");// remove active class from all li's
        $(this).addClass("active");// add highlight to clicked link
    });
});

1 Comment

@user2720197 test the above answer I've made changes in it.
0

It is working as charm

Need a change at line 21 in your code.

Use:

$("ul li ul li").click(function() {  

instead of

$("ul li").click(function() { 

Comments

0

Try this to your script :

jquery 10 version

$(document).ready(function(){
    $('ul li ul li').on('click', function() {
        $('ul li ul li').removeClass('active');
        $(this).addClass('active');
    });
});

oops sorry if you use lower version of jquery

jquery 1.3 will be fine

    $(document).ready(function(){
        $('ul li ul li').live('click', function() {
            $('ul li ul li').removeClass('active');
            $(this).addClass('active');
        });
    });

EDIT

Do you want something like this :

var Test = {
    init:function() {
        $('ul li').click(function() {
            $('li').removeClass('active');
            $(this).addClass('active');
            $(this).children().css({
                'background' : '#fff',
                'color' : '#000'
            });
        });
    }
}

$(document).ready(function(){
    Test.init();
});

2 Comments

You can choose based on what jquery version you use
Do you also want to have the parent an active class?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.