How can I add class active to li a current day of the week?
<ul class="nav nav-tabs nav-justified responsive" id="myTab">
<li class="active">Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
</ul>
If you add active to the root <ul> element, all children will inherit the active class.
<ul class="nav nav-tabs nav-justified responsive active" id="myTab">
<li>Monday</li>
<li>Tuesday</li>
<li>Wednesday</li>
<li>Thursday</li>
<li>Friday</li>
</ul>
You can do
li.active {
/* your CSS goes here */
}
to only select li elements with the class active in your CSS.
You can do it programatically using javascript, calculating the day and adding the "active" attribute to the element that contains the day of the week indicated.
Here you have the script i've been working so far:
HTML:
<ul class="nav nav-tabs nav-justified responsive" id="myTab">
<li name="Monday">Monday</li>
<li name="Tuesday">Tuesday</li>
<li name="Wednesday">Wednesday</li>
<li name="Thursday">Thursday</li>
<li name="Friday">Friday</li>
<li name="Saturday">Saturday</li>
<li name="Sunday">Sunday</li>
</ul>
As you can see, I added the name attribute to each li element. I do this because we're going to use them in our script below.
Javascript:
var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
var daysOfTheWeek = document.getElementsByTagName("li");
dayOfTheWeek(daysOfTheWeek);
function dayOfTheWeek (weekDays) {
for(var i = 0; i < weekDays.length; i++)
{
if(weekDays[i].getAttribute("name") == n)
{
weekDays[i].className = 'active';
weekDays[i].childNodes[0].className = 'active';
}
}
}
Here, I created a Date object that will give us the name of the day of the week with the help of the weekday array.
Once we have the name of the weekday, we call the dayOfTheWeek method, which will set active the elements we desire (the weekday of the list), turning its color into red (see CSS code below).
CSS:
li.active
{
color: red;
}
a.active
{
color: red;
}
Here we only created two rules to see what happens when we make our desired element active.
You can find my code here: https://jsfiddle.net/a0yjLc5z/6/
Hope it helps!
Related
I am attempting to create a way of ordering a list into specific sections using a nav bar. For example, all list items displays under 'All' and then the option to refine the list into 3 other sections.
Current example
https://ibb.co/pvBV4Lf
Does anyone have any suggestions on how to achieve this? Any examples?
I'm not even sure what you would name this kind of function
<div class="nav">
<h1>All</h1>
<h1><span>Residential</span></h1>
<h1><span>Commercial</span></h1>
<h1><span>Retail</span></h1>
</div>
<ul>
<li>Granville Place</li><br>
<li>Palisade</li><br>
<li>King & Phillip</li><br>
<li>Castle Residences</li><br>
<li>Opera Residences</li><br>
<li>Brighton Collection</li><br>
<li>Carpe Group</li><br>
<li>The Lennox</li><br>
<li>South Quarter</li><br>
<li>One Barangaroo</li><br>
<li>One Queensbridge</li><br>
<li>Australia 108</li><br>
<li>Oceans Freshwater</li><br>
<li>The Pavilions</li>
</ul>
Welcome to stackoverflow,
one way to filter your list element is to append a data-* attribute to your elements and bind a javascript function on your links in the div.nav to filter the list:
window.addEventListener('DOMContentLoaded', () => {
// filter list
const filterList = e => {
e.preventDefault();
// fetch filtertype
const type = e.target.closest('a').getAttribute('data-filter-type');
// hide or display list elements
document.querySelectorAll('ul li').forEach(li => {
li.style.display = type === li.getAttribute('data-filter-type') || type === 'all' ? 'list-item' : 'none';
});
};
// bind links
document.querySelectorAll('.nav a').forEach(a => {
a.addEventListener('click', filterList)
});
});
h1 { font-size: 18px; }
<div class="nav">
<h1>All</h1>
<h1><span>Residential</span></h1>
<h1><span>Commercial</span></h1>
<h1><span>Retail</span></h1>
</div>
<ul>
<li data-filter-type="residential">Granville Place</li>
<li data-filter-type="residential">Palisade</li>
<li data-filter-type="commercial">King & Phillip</li>
<li data-filter-type="commercial">Castle Residences</li>
<li data-filter-type="residential">Opera Residences</li>
<li data-filter-type="residential">Brighton Collection</li>
<li data-filter-type="commercial">Carpe Group</li>
<li data-filter-type="retail">The Lennox</li>
<li data-filter-type="retail">South Quarter</li>
<li data-filter-type="residential">One Barangaroo</li>
<li data-filter-type="residential">One Queensbridge</li>
<li data-filter-type="retail">Australia 108</li>
<li data-filter-type="commercial">Oceans Freshwater</li>
<li data-filter-type="commercial">The Pavilions
</ul>
I made a menu for umbraco but I can't get the dropdown to work! Below is my code:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
<div class="NavDigiCampuz">
<div>
<div class="nav nav-list tree">
#{
var documentRootNodeId = Model.Content.GetPropertyValue("documentRoot", true); // Fetch recursive document root id.
var selection = Umbraco.TypedContent(documentRootNodeId).Children.Where("Visible"); // Select all nodes below the document root.
}
#foreach(var item in Model.Content.Ancestor(1).Descendants("menu")){
foreach (var Menus in #item.Descendants("menuItem")){
<li class="MenuItems">#Menus.Name</li>
foreach (var MenuItems in #Menus.Children){
if(MenuItems.GetPropertyValue("hoofdstukIcoon") != "") {
<li><a class="NormalA" href="#MenuItems.Url"><i class="fa fa-#(MenuItems.GetPropertyValue("hoofdstukIcoon")) fa-fw"></i> #MenuItems.Name</a></li>
}else {
<li><a class="NormalA"href="#MenuItems.Url">#MenuItems.Name</a></li>
var childIsActive = false;
foreach(var sub in #MenuItems.Children){
if (CurrentPage.Url == sub.Url) {
childIsActive = true;
}
<ul class="nav subnav nav-list tree #(childIsActive ? "" : "collapse")">
#foreach(var sub2 in #MenuItems.Children){
<li #(CurrentPage.Url == sub.Url ? "class=active" : "")>
<a onclick="parent.resizeParent();" href="#sub.Url">#(sub.GetPropertyValue("hoofdstukMenuTitel") != "" ? sub.GetPropertyValue("hoofdstukMenuTitel") : sub.GetPropertyValue("hoofdstukTitel"))</a>
</li>
}
</ul>
}
}
}
}
}
</ul>
</div>
</div>
</div>
The dropdown code should start at <ul class="nav subnav nav-list tree #(childIsActive ? "" : "collapse")"> but that doesn't seem to work.
Below is a quick structure of how I want my page to be:
Help item 3 should have a dropdown which it doesn't have right now. What am I missing?
You have some inconsistencies in your structure:
this line:
<div class="nav nav-list tree">
should be this:
<ul class="nav nav-list tree">
And I would remove the <div> that wrapps it.
I'm just guessing you are using something like bootstrap, are you using a framework or custom css?
Your submenu should also be nested in the <li> element:
else
{
<li><a class="NormalA" href="#MenuItems.Url">#MenuItems.Name</a>
var childIsActive = false;
foreach (var sub in #MenuItems.Children)
{
if (CurrentPage.Url == sub.Url)
{
childIsActive = true;
}
<ul class="nav subnav nav-list tree #(childIsActive ? "" : "collapse")">
#foreach (var sub2 in #MenuItems.Children)
{
<li #(CurrentPage.Url == sub.Url ? "class=active" : "")>
<a onclick="parent.resizeParent();" href="#sub.Url">#(sub.GetPropertyValue("hoofdstukMenuTitel") != "" ? sub.GetPropertyValue("hoofdstukMenuTitel") : sub.GetPropertyValue("hoofdstukTitel"))</a>
</li>
}
</ul>
}
</li>
}
I am working on drop down in angular js which is already in ng-repat option.
I want to make it work in a way that when an option is selected in any of repeated select box it should not be available in other repeated select boxes.
My current code looks like this. :
HTML:
<div ng-repeat="endDate in endDate_counts">
<div class="sch_endDate_opsAmount_cell cell1">
<strong class="nc_Camaignfinance_title">Option</strong>
<div class="nc_Camaign_costFinance_opsSlct" select-input>
<div class="nc_Camaign_costFinance_opsSlct_inn">
<span>{{endDate.select_endDate}}</span>
<ul>
<li ng-click="endDate.select_endDate = 'Never'">
<em>Never</em></li>
<li ng-click="endDate.select_endDate = 'Impressions'">
<em>Impressions</em></li>
<li ng-click="endDate.select_endDate = 'Budget'">
<em>Budget</em></li>
<li ng-click="endDate.select_endDate = 'Clicks'">
<em>Clicks</em></li>
<li ng-click="endDate.select_endDate = 'Date'">
<em>Date</em></li>
</ul>
</div>
</div>
</div>
</div>
*Note: Please ignore the custom directives or other methods. I am using them myself and aware of them .
try:
html:
<ul>
<li ng-show="checkExist(key-1,'Never')" ng-click="endDate.select_endDate = 'Never'">
<em>Never</em></li>
<li ng-show="checkExist(key-1,'Impressions')" ng-click="endDate.select_endDate = 'Impressions'">
<em>Impressions</em></li>
<li ng-show="checkExist(key-1,'Budget')" ng-click="endDate.select_endDate = 'Budget'">
<em>Budget</em></li>
<li ng-show="checkExist(key-1,'Clicks')" ng-click="endDate.select_endDate = 'Clicks'">
<em>Clicks</em></li>
<li ng-show="checkExist(key-1,'Date')" ng-click="endDate.select_endDate = 'Date'">
<em>Date</em></li>
add this function in your js file:
$scope.checkExist = function(index,value){
if(index < 0){
return true;
}
if($scope.endDate_counts[index].select_endDate == value){
return false;
}else {
return true;
}
}
I have been trying to work out how to produce a list, where if you click on one of the objects in the list, it becomes active. I have managed to get it working in the non-dynamic version of the code with ease. However, working on AngularJS dynamic version, I just can't seem to get it to work. The data is ok, so it can't be data related, it has to be my dynamic code that is not working.
This is a working piece of code (however not dynamic)
<ul class="nav nav-pills" ng-init="catVal = 1">
<li ng-class="{active: catVal===1}">
Category 1
</li>
<li ng-class="{active: catVal===2}">
Category 2
</li>
<li ng-class="{active: catVal===3}">
Category 3
</li>
</ul>
Now What I really want is an AngularJS dynamic version of this code. This is what I have tried and failed so far.
<ul class="nav nav-pills">
<li ng-repeat="category in model" ng-init="catVal = 1" ng-class="active: catVal === category.catID ">
{{category.catName}}
</li>
</ul>
The catID is associated with the category and should give the same results as the previous list. I get the names in the correct place, just the value is not working.
try this.
var app = angular.module("app",[]);
app.controller("ctrl" , function($scope){
$scope.rowIndex = -1;
$scope.items = [{"name":"ali","score":2},{"name":"reza","score":4},{"name":"amir","score":5},{"name":"asd","score":10}];
$scope.selectRow = function(index){
if(index == $scope.rowIndex)
$scope.rowIndex = -1;
else
$scope.rowIndex = index;
}
});
.active{
background-color:#a11af0;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl" class="panel-group" id="accordion">
<ul class="nav nav-pills" ng-init="catVal = 1">
<li ng-repeat="item in items" ng-class="{'active':rowIndex == $index }" ng-click="selectRow($index)">
{{item.name}}
</li>
</ul>
</div>
I am using bootstrap nav bar with codeigniter as backend.
Now In my application i want to change the < li> class to active dynamically for each page.
So i can known i am on which page currently.
Below is my code:
<ul class="nav navbar-nav">
<li class="active">Dashboard</li>
<li>Company</li>
<li>Users Management</li>
<li>Key Management</li>
<li>Activated Devices</li>
</ul>
<ul class="nav navbar-nav navbar-right">
You can use this javascript to add active class based on current url please try it
<script type="text/javascript">
$(document).ready(function () {
var url = window.location;
$('ul.nav a[href="'+ url +'"]').parent().addClass('active');
$('ul.nav a').filter(function() {
return this.href == url;
}).parent().addClass('active');
});
</script>
Can't you just use this replacing your <a> tags. This will match the current uri string, with the links href attribute. If it matches, it add's an active class to the link. You can style the .active using you
Company
So the entire code will be.
<ul class="nav navbar-nav">
<li>Dashboard</li>
<li>Company</li>
<li>Users Management</li>
<li>Key Management</li>
<li>Activated Devices</li>
</ul>
<ul class="nav navbar-nav navbar-right">
Your pages should know which menu should be set as active.
Try adding an id to your <li> and add a javascript code to your pages to set the active menu based on the ID.
Example:
<ul class="nav navbar-nav">
<li id="menu-dashboard" class="active">Dashboard</li>
<li id="menu-company">Company</li>
<li id="menu-user">Users Management</li>
<li id="menu-key-management">Key Management</li>
<li id="menu-activation">Activated Devices</li>
</ul>
<script>
var menuid = "menu-dashboard";
$(function() {
$('.nav li').removeClass('active');
$(menuid).addClass("active");
});
</script>
Pages:
Add the following javascript lines to your pages
Company page
menuid = "#menu-company";
User page
menuid = "#menu-user";
You can achieve it by using jQuery or JavaScript.
jQuery example:
$('.nav li').click(function(){
$('.nav li').removeClass('active');
$(this).addClass('active');
});
The jQuery in the accepted answer do not know the difference if the visitor is coming from http://example.com or http://example.com/index.php and would not activate the "home" menu-item. This is my fix for that.
HTML This is a snippet from my menu:
<div class='navbar'>
<a href='index.php'><div class='nav-icon icon-home'></div>Hem</a>
<a href='calender.php'><div class='nav-icon icon-calender'></div>Kalender</a>
<a href='exchange.php#utbyte'><div class='nav-icon icon-byte'></div>Byten</a>
</div>
This is a function I've added to my database controller class to find out what protocol the visitor use:
public static function adr_protocol()
{
$str = 'http://';
if(isset($_SERVER['HTTPS']) && 'on' === $_SERVER['HTTPS']) $str = 'https://';
return $str;
}
jQuery:
;var ix = '<?php echo DBController::adr_protocol()."{$_SERVER['HTTP_HOST']}/"; ?>';
var url = (window.location==ix)?window.location+'index.php':window.location;
$('div.navbar a[href="'+ url +'"]').parent().addClass('active');
$('div.navbar a').filter(function() {
return this.href == url;
}).addClass('active');