Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
145
rated 0 times [  151] [ 6]  / answers: 1 / hits: 184298  / 11 Years ago, tue, january 14, 2014, 12:00:00

I have a menu with certain items and I want when a user clicks on any li than only its class becomes active. I have a menu items like following:


<ul class="nav">
<li class="dropdown active">
<asp:HyperLink ID="hlHome" runat="server" href="Default.aspx">Home</asp:HyperLink>
</li>
<li class="dropdown">
<asp:HyperLink runat="Server" cssClass="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" data-delay="0" data-close-others="false" href="#">
Register
<i class="icon-angle-down"></i>
</asp:HyperLink>
<ul class="dropdown-menu">
<li><asp:HyperLink runat="server" ID="hlCreateAccount" href="register.aspx">Create Account</asp:HyperLink></li>
<li><asp:HyperLink runat="Server" href="forgot.aspx" ID="hlForgot">Forgot Credentials ?</asp:HyperLink></li>
<li><asp:HyperLink runat="server" href="blocked.aspx" ID="hlBlockedAccount">Blocked Account</asp:HyperLink></li>
<li><asp:HyperLink ID="hlUnblockAccount" href="unblock.aspx" runat="server">Unblock Account</asp:HyperLink></li>
</ul>
</li>
<li><asp:HyperLink ID="hlBug" runat="server" href="bug.aspx">Report A Bug</asp:HyperLink></li>
<li><asp:HyperLink ID="hlContact" runat="server" href="contact.aspx">Contact Us</asp:HyperLink></li>
</ul>

And I have tried the following code with jQuery:


<script type="text/javascript">
function pageLoad() {
var loc = window.location.href.split('/');
var page = loc[loc.length - 1];
$('ul.nav a').each(function (i) {
var href = $(this).attr('href');
if (href.indexOf(page) !== -1) {
$('ul.nav li.active').removeClass('active');
$(this).parent().addClass('active');
}
});
}
</script>

It's not working with the drop-down menus while working perfectly with all other menus not having drop-down items. How can I change the code that it works too with menu items having a drop-down list of items? I am also using the update panel on my page.


More From » jquery

 Answers
6

You specified both jQuery and Javascript in the tags so here's both approaches.



jQuery



var selector = '.nav li';

$(selector).on('click', function(){
$(selector).removeClass('active');
$(this).addClass('active');
});


Fiddle: http://jsfiddle.net/bvf9u/






Pure Javascript:



var selector, elems, makeActive;

selector = '.nav li';

elems = document.querySelectorAll(selector);

makeActive = function () {
for (var i = 0; i < elems.length; i++)
elems[i].classList.remove('active');

this.classList.add('active');
};

for (var i = 0; i < elems.length; i++)
elems[i].addEventListener('mousedown', makeActive);


Fiddle: http://jsfiddle.net/rn3nc/1






jQuery with event delegation:



Please note that in approach 1, the handler is directly bound to that element. If you're expecting the DOM to update and new lis to be injected, it's better to use event delegation and delegate to the next element that will remain static, in this case the .nav:



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


Fiddle: http://jsfiddle.net/bvf9u/1/



The subtle difference is that the handler is bound to the .nav now, so when you click the li the event bubbles up the DOM to the .nav which invokes the handler if the element clicked matches your selector argument. This means new elements won't need a new handler bound to them, because it's already bound to an ancestor.



It's really quite interesting. Read more about it here: http://api.jquery.com/on/


[#73181] Monday, January 13, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ninaemiliaj

Total Points: 405
Total Questions: 112
Total Answers: 112

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
;