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>
$("*").removeClass("active");should be replaced with$(".active").removeClass("active");as it's way more efficient.activeclasses if that's part of your eventual solution.