How to change active class at each link click in jQuery? - javascript

I have an MVC app, where in the _Layout.cshtml file I have something like this:
<div class="collapse navbar-collapse">
<ul class="nav pull-right navbar-nav">
<li id="home" class="active"><a id="homeLink" href='#Url.Action("Index", "Home")'>Home</a></li>
<li id="about"><a id="aboutLink" href='#Url.Action("About", "Home")'>About Us</a></li>
<li><a href='#Url.Action("Services", "Home")'>Services</a></li>
<li><a href=''>Blog</a></li>
<li><a href='#Url.Action("Contact", "Home")'>Contact</a></li>
</ul>
</div>
So the point is that when the website starts, the first link is set to active, then after each link click I want to set that one's class to active. I guess I need to write some jQuery code, but I cannot seem to figure out the answer.
UPDATE:
<ul class="nav pull-right navbar-nav">
<li id="Home" class="active"><a href='#Url.Action("Index", "Home")'>Home</a></li>
<li id="About"><a href='#Url.Action("About", "Home")'>About Us</a></li>
<li id="Services"><a href='#Url.Action("Services", "Home")'>Services</a></li>
<li id="Blog"><a href=''>Blog</a></li>
<li id="Contact"><a href='#Url.Action("Contact", "Home")'>Contact</a></li>
</ul>
main.js
$(function () {
$('a').click(function () {
$(this).closest('ul').find('li').removeClass('active');
$('#' + '#ViewBag.Title').addClass('active');
});
});
Example views:
#{
ViewBag.Title = "Services";
}
#{
ViewBag.Title = "Contact";
}

The following script will add the active class
$('a').click(function(e) {
// uncomment the following line to prove it
// e.preventDefault();
$(this).closest('ul').find('li').removeClass('active');
$(this).closest('li').addClass('active');
})
however, since your clicking on a link you don't even see it because you immediately redirect to another page using the same layout where the active class applied to the first li element (the new view has no knowledge of what you did on this page).
Since you seem to be wanting to highlight the li element relating to the current page, one option would be to give your li elements an id attribute matching the ViewBag.Title property and use that to apply the class.
<div class="collapse navbar-collapse">
<ul class="nav pull-right navbar-nav">
<li id="home">#Html.ActionLink("Home", "Index", "Home")</li>
<li id="about">#Html.ActionLink("About Us", "About", "Home")</li>
<li id="services">#Html.ActionLink("Services", "Services", "Home")</li>
....
</ul>
</div>
and the script
$('#' + '#ViewBag.Title').addClass('active');
Then in the Index.cshtml view,
#{
ViewBag.Title = "Home"; // or "About" for About.cshtml, "Services" for Services.cshtl etc.
Layout = "~/Views/Shared_Layout.cshtml";
}

Assuming you aren't doing anything to alter the way the links fire (loading content with ajax), you can compare the href of the links to the current url to figure out which one should be active.
$('.navbar-nav ul').children().each(function() {
var link = $(this).find('a'); // or var link = $(this).children('a');
if (link.attr('href') == location.href) {
$(link).addClass('active');
return false;
}
});
All you'll need is to make sure the link href is formatted the same way it will appear in the url (relative vs absolute).

Related

Load dynamic content in div from different navbars

On one page, alphabet.php, in my project I have a sidebar further down on the page that loads diverse php-pages in a Content div. Like this:
HTML SideMenu
<nav id="sideMenu">
<ul class="list-unstyled components">
<li id="a">A</li>
<li id="b">B</li>
<li id="c">C <span class="caret"></span>
<ul>
<li id="c1">C1</li>
<li id="c2">C2</li>
</ul>
</li>
</ul>
</nav>
<div id="Content">
<h2>Lorem bla bla</h2>
<p>lorem bla bla </p>
</div>
JS
$(function() {
'use strict';
var newHash = '',
$content = $("#Content");
$("#sideMenu").delegate("a", "click", function() {
window.location.hash = $(this).attr("href");
return false;
});
$(window).bind('hashchange', function() {
newHash = window.location.hash.substring(1);
$content.load(newHash);
console.log(newHash);
});
});
The same content should be loaded also when you click the topmenu and footer navigation. And at the same time scroll to the Content div.
HTML MainMenu
<li class="dropdown"><a href="#alpha" class="dropdown-toggle" data-
toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
Alphabet <span class="caret"></span></a>
<ul class="dropdown-menu dropdown-menu-left" role="menu" id="alpha">
<li>A
</li>
<li>B
</li>
<li>C
<ul class="dropdown-menu dropdown-menu-left" role="menu">
<li>C1</li>
<li>C2</li>
</ul>
</li>
</ul>
</li>
I solved the JS for the main-menu with this code
$(function() {
'use strict';
if(location.hash) $('#Content').load(location.hash.substring(1));
$('#alphaa').click(function() {
$('#Content').load(this.hash.substring(1));
});
});
EDIT: I changed the URL in MainMenu to alphabet.php#a.php etc., and it loads the content into the div IF you are on the current page (alphabet.php). Not if you're on another page.
EDIT 2: Solution for the main-menu JS. Now everything works fine but I guess you could make it prettier without two different js functions for the main-menu and the side-menu, but for now I'm pleased with this. It works!
Here is how i am able to do the navigation
i wrote the links like:
<a class="nav-link" href="javascript:void(0);" data-href="/page1.html #div1">Page 1</a>
<a class="nav-link" href="javascript:void(0);" data-href="/page2.html #div2">Page 2</a>
<a class="nav-link" href="javascript:void(0);" data-href="/page3.html #div3">Page 3</a>
If you use javascript:void(0) in your href= then you don't need to call the event.preventDefault.
then in my scripting side i wrote:
$(window).on('load', function () {
$('.nav-link').on('click', function () {
var linkPage = $(this).attr('data-href');
if (linkPage !== undefined)
{
$('#content').load(linkPage);
}
});});
Note
One thing you need to make sure is that domain of the calling page and pages from which the data has to come should be the same or else you will have the cross-domain issue.
Edit 1
The #div1,#div2,.#div3 are basically the ids of the div on their respective pages from which the data has to be fetched.
for example if i have some content on my pages like this:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Page 1</title>
</head>
<body>
<div id="div1">
hi i am the content of page 1
</div>
</body>
</html>
So, when someone clicks on the menu links the load function will go to the particular page (which is in our case the page1.html or any other) and return the HTML contents of div with id as div1 and load the data to the particular div(i.e. div with id content).
Edit 2
The JQuery's Load() is better explained on this Jquery Load you can use this to understand more about it.
So, what i think you are doing is you are creating a request to a /page1.html from the domain www.example.com, in that case load function will call the www.example.com/page.html and will load the contents of the page but if you make a request to other any other page let say /page11.html from the domain www.example.com/page2.html then the load() will create this www.example.com/page2.html/page11.html url which is not correct and hence it will not load any content.
So,what you can do is either you can put the whole url on the of the page i.e. www.yourdomain.com/page.html #divid in the attribute data-href or you can create the particular url on the javascript calling funciton like:
$(window).on('load', function () {
$('.nav-link').on('click', function () {
var linkPage = $(this).attr('data-href');
if (linkPage !== undefined)
{
var pageUrl = window.location.origin+'any other seperation for the pages
if any'+linkPage;
$('#content').load(pageUrl);
}
});
});
Here the window.location.origin is going to give you the domain on which the website is running regardless of hashes or slashes and then it will concate it with the linkPage and will make the proper url for the called page.

Creating sub menu below toggle menu item for mobile versions

I use the following code to create a toggle menu for mobile versions and It works fine but I want to generate a sub menu below a menu item. For example I want to have to create two sub tabs for SERVICES item like New and Used to be appeared only when the user clicks on SERVICES. Anybody can help me doing this?
HTML
<nav id="navigation">
<a class="menu_button" href="#footer_nav" onclick="toggleNav(); return false;">☰ MENU</a>
<ul id="navigation_list" role="navigation">
<li><a href=#>HOME</a></li>
<li><a href=#>SERVICES</a></li>
<li><a href=#>WORK</a></li>
<li><a href=#>CONTACT</a></li>
</ul>
</nav>
Javascript
var originalNavClasses;
function toggleNav() {
var elem = document.getElementById('navigation_list');
var classes = elem.className;
if (originalNavClasses === undefined) {
originalNavClasses = classes;
}
elem.className = /expanded/.test(classes) ? originalNavClasses : originalNavClasses + ' expanded';
}
From http://blog.g-design.net/post/42617934013/create-an-accessible-toggle-menu-for-mobile
Thanks
You simply just create another list inside of the SERVICES list item. You can initially hide the list, and when you want to see it, via clicking, you just display it.
<nav id="navigation">
<a class="menu_button" href="#footer_nav" onclick="toggleNav(); return false;">☰ MENU</a>
<ul id="navigation_list" role="navigation">
<li><a href=#>HOME</a></li>
<li id="services-item">SERVICES
<ul id="sub_list" style="display: none;">
<li>NEW</li>
<li>USED</li>
</ul>
</li>
<li><a href=#>WORK</a></li>
<li><a href=#>CONTACT</a></li>
</ul>
</nav>
If you want to see the list when you click SERVICES, you need to add javascript, or a JS library, to do so:
//jQuery
$('#services-item').on('click', function() {
$('#sub-list').toggle();
)};
This method does not allow SERVICES to direct the user to a page. If you need it to direct a user to a page, then just add an icon beside the SERVICES text that toggles the sub-list when clicked.

jQuery click() and on() function

I'm trying to add "active" class to menu when user clicks on the button but for some reason it's not working correctly. They have to click really fast and two times. I've tried both click and on click function but still not working.
$('#menu li').click(function() {
$('#menu li.active').removeClass('active');
$(this).addClass('active');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right" id="menu">
<li class="nav active">Home
</li>
<li class="nav">About
</li>
<li class="nav">Services
</li>
<li class="nav">Gallery
</li>
<li class="nav">Contact
</li>
</ul>
</div>
you must handle active li after page loaded.Add this at the end of your code:
var curruntLocation = window.location.href;
$('#menu li a').each(function ()
{
var thisHref = $(this).attr('href');
if (curruntLocation.indexOf(thisHref) > 0)
{
$(this).parent().addClass('active');
}
});
The links included in the menu items are reloading the page while clicking and when the page is reloaded, the initial setting comes first. You can use preventDefault() if you'd like to run the function but then, your links won't work.
I suggest you use anchors instead of query string.

dropdown menu issue in mobile

I have a problem with my menu in mobile mode. onClick it fadesOut. I want to keep this setting, however, I want it to do nothing, when one clicks on the dropdown part of the menu.
here is link: http://jsfiddle.net/zLLzrs6b/3/
appreciate your help!
also my html:
<nav id="nav-wrap">
<a class="mobile-btn" href="#nav-wrap" title="Show navigation">Show Menu</a>
<a class="mobile-btn" href="#" title="Hide navigation">Hide Menu</a>
<ul id="nav" class="nav">
<li><a class="smoothscroll mobile" href="#about">about</a></li>
<li><a class="smoothscroll mobile" href="#documents">blog</a></li>
<li class="nav-item">dropdown
<ul class="langop">
<li>otion 1</li>
<li>otion 2</li>
</ul>
</li>
</ul>
</nav>
css:
.langop
{
display:none;
position: relative;
width:auto;
}
.nav-item:hover .langop {
display: block;
}
java:
var toggle_button = $("<a>", {
id: "toggle-btn",
html : "Menu",
title: "Menu",
href : "#" }
);
var nav_wrap = $('nav#nav-wrap')
var nav = $("ul#nav");
nav_wrap.find('a.mobile-btn').remove();
nav_wrap.prepend(toggle_button);
toggle_button.on("click", function(e) {
e.preventDefault();
nav.slideToggle("fast");
});
if (toggle_button.is(':visible')) nav.addClass('mobile');
$(window).resize(function(){
if (toggle_button.is(':visible')) nav.addClass('mobile');
else nav.removeClass('mobile');
});
$('ul#nav li a').on("click", function(){
if (nav.hasClass('mobile')) nav.fadeOut('fast');
});
You have several issues here, but the important ones are:
There is no "nav" variable being set. I think you are confusing the $() selector with this "nav" variable.
There is no "mobile" class on your anchor elements.
You can avoid the if clause by simply specifying the anchor class right in the selector: $(a.mobile) You can see a working version of it here: http://jsfiddle.net/zLLzrs6b/ I've added the "mobile" classes to your anchors and cleaned up the jQuery.

how to active parent ul li after page refresh or click on sub menu link

how do i keep selected tab active after page reload or refresh i have working on drop line navigation menu
function is working fine for parent ul li and also parent active when click on parent link
but problem is that when i click on sub menu link and redirect to another page then clicked parent ul li didn't active and every-time Home Tab active or highlight whenever page refresh or redirect to another page
for example i want like this
Account Menu is parent menu and child menu like user profile and user accounts
when i click user profile or user accounts script should active or highlight
Account Menu Tab Not Home Tab
Here Javascript
<script type='text/javascript'>//<![CDATA[
$(window).load(function(){
$(function () {
$("li:first-child").addClass("active");
$('li').click(function () {
$('.secondary li').removeClass('active');
$(this).addClass('active');
});
})
});//]]>
</script>
Html Code
<div role="navigation" id="navPrimary">
<ul class="secondary">
<li >Home</li>
<li>Account Menu
<ul>
<li>OVERVIEW</li>
<li>EDIT PROFILE</li>
<li>MANAGE EMAILS</li>
<li>EDIT PASSWORD</li>
<li>CREDIT CARDS</li>
<li>BANK ACCOUNTS</li>
<li>CLOSE ACCOUNT</li>
<li>LOGOUT</li>
</ul>
</li>
<li>Payments Menu
<ul>
<li>OVERVIEW</li>
<li>EDIT PROFILE</li>
<li>MANAGE EMAILS</li>
<li>EDIT PASSWORD</li>
<li>CREDIT CARDS</li>
<li>BANK ACCOUNTS</li>
<li>CLOSE ACCOUNT</li>
<li>LOGOUT</li>
</ul>
</li>
<li>Request Money
<ul>
<li>Manage Invoices</li>
<li><a href="" >Request Money</a></li>
<li><a href="" >Create Invoice</a></li>
<li><a href="" >Invoice Settings</a></li>
</ul>
</li>
<li><a href="#" >Merchant Services</a></li>
<li><a href="#" >Products & Services</a></li>
</ul>
</div>
Updated Javascript but not working
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
$(document).ready(function () {
var index = Cookies.get('active');
$('.secondary').find('a').removeClass('active');
$(".secondary").find('a').eq(index).addClass('active');
$('.secondary').on('click', 'li a', function (e) {
e.preventDefault();
$('.secondary').find('a').removeClass('active');
$(this).addClass('active');
Cookies.set('active', $('.clearfix a').index(this));
});
});
}//]]>
</script>
You will definitely store somewhere the data, wich was the last active tab. If you want to do this on client side only, one solution can be the cookies. See here: How do I set/unset cookie with jQuery?
If you are using HTML5, then you can use web storage. See here.
If you are rendering your page by PHP, you can use $_SESSION variable for this. When user clicks a tab, you make an ajax request, and store the value of the active tab in a $_SESSION variable, but as i see, you want to do it on the client side.
UPDATE
Ok, i just created it for you.
First thing is to set an id for every a element in my example, to know, wich is that. You can do it some other way, but now this is the most simple.
Second is to download the jquery.cookie.js, and do not use the URL, store it locally.
Third, here could be a problem, if cookies are disabled on the client machine.
You need to do something like this:
HTML
<div role="navigation" id="navPrimary">
<ul class="secondary">
<li>Home</li>
<li>Account Menu
<ul>
<li>OVERVIEW</li>
</ul>
</li>
</ul>
</div>
CSS
.active {font-weight: bold; color: #F00}
JQUERY
ok, stacoverflow does not allow me to paste here a code but i loaded here the
https://raw.githubusercontent.com/carhartl/jquery-cookie/master/src/jquery.cookie.js script also!
<script type='text/javascript'>
$(function() {
//$.removeCookie("active");
var activeMenu = $.cookie("active");
console.log(activeMenu);
if (typeof activeMenu === 'undefined') {
$("#home").addClass("active");
} else {
$('#' + activeMenu).addClass('active');
}
$('li a').click(function(e) {
e.preventDefault();
var listItems = $("#navPrimary li a");
listItems.each(function(idx, li) {
$(li).removeClass('active');
});
$(this).addClass('active');
var id = $(this).attr('id');
$.cookie("active", id);
});
})
</script>

Categories

Resources