How to apply a style on links with querystrings - html

So basically if you want an indicator on the link of the page you are currently on you just apply a style on the page for example:
<li>HOME</li>
<li>ABOUT</li>
<li>CONTACT</li>
It will apply whatever style is on the class active when you are in the home.aspx page...
Now I have something similar but this time, instead of directing to another page, the links will just redirect on the same page but filtered with querystrings...
I have:
<li>PROJECT 1</li>
<li>PROJECT 2</li>
<li>PROJECT 3</li>
What I would like to happen is I want to apply a css style when I click one of those links so that people know which project they are looking at.

Just add a little jquery to add the .active class to selected element.
<script>
jQuery(function($){
var url = window.location.href;
// give the li or a tag a class
$('.element-class-name a[href="'+ url +'"]').addClass('active');
$('.element-class-name a').filter(function() {
return this.href == url;
}).addClass('active');
});
</script>

Try caching selector $("a[href^=projects]") as variable , attach click event to cached selector , utilize .each() to iterate all elements in collection , set className to "active" if this current element === event.target
var a = $("a[href^=projects]");
a.click(function(e) {
a.each(function() {
this.className = this === e.target ? "active" : ""
})
})
var a = $("a[href^=projects]");
a.click(function(e) {
// Note, `e.preventDefault()` included for stacksnippets
e.preventDefault();
a.each(function() {
this.className = this === e.target ? "active" : ""
})
})
a.active {
color: red;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>PROJECT 1
</li>
<li>PROJECT 2
</li>
<li>PROJECT 3
</li>
</ul>

Doing something like the following works also:
$('.link').on('click', function(e) {
$('.links li .link').removeClass('active');
$(this).addClass('active');
});
Where link is the class I gave the actual link within a list called links.
The example is here: http://codepen.io/anon/pen/OyJgBQ

Related

why document.getElementsByName().length always return 0?

I'm new to JavaScript. In the following code getElementsByName("li").length always returns 0 although there are many <li>-tags in my HTML, why?
document.addEventListener('DOMContentLoaded', function() {
var len = document.getElementsByName('li').length;
alert(len);
})
art of my HTML:
<body>
<ul>
<li>aaaaaa</li>
<li>bbbbbb</li>
<li>cccccc</li>
</ul>
</body>
Replace
document.getElementsByName('li')
with
document.getElementsByTagName('li')
This is happening cause you are selecting by tag name and not by name ! You are using wrong function!
The method you are attempting to use is trying to find a specific element by its name.
None of your list items have a name, to do this you should update your code so that your items have names.
document.addEventListener('DOMContentLoaded', function() {
var len = document.getElementsByName('list_item_1').length;
alert(len);
})
<li name="list_item1">aaaaaa</li>
You can do something like this because getElementsByTagName() returns NodeList so you can iterate over it like an array or get the length.
document.addEventListener('DOMContentLoaded', function() {
var listElements = document.getElementById('list').getElementsByTagName("li");
alert(listElements.length);
})
<body>
<ul id="list">
<li >aaaaaa</li>
<li>bbbbbb</li>
<li>cccccc</li>
</ul>
</body>
getElementsByName() although also returns NodeList but it returns list of all same name elements in the whole document so to make this work you need to give same name to all list items.
document.addEventListener('DOMContentLoaded', function() {
var len = document.getElementsByName('name').length;
alert(len);
});
<body>
<ul>
<li name="name">aaaaaa</li>
<li name="name">bbbbbb</li>
<li name="name">cccccc</li>
</ul>
</body>

Highlight selected components VueJS

I'm working in Vue file, and what i want is to highlight the active element, what would be the best way to accomplish it?
code
<li #click = "selectedComponent = 'appBugs1'"><i class="ion-bug"></i>Test 1</li>
<li #click = "selectedComponent = 'appBugs2'"><i class="ion-bug"></i>Test 2</li>
<li #click = "selectedComponent = 'appBugs3'"><i class="ion-bug"></i>Test 3</li>
So lets say the first "li" element was selected -> it should give red background to "li", and if another one selected, it must reset the first one and assign the red background to the new selected element.
I tried to search on the web, but there is nothing much about it, it would be easy if you have only 2 options, but my list is much larger. So what would be the best way to solve this?
You could do it like this.
<li #click = "selectComponent('appBugs1', $event)"><i class="ion-bug"></i>Test 1</li>
<li #click = "selectComponent('appBugs2', $event)"><i class="ion-bug"></i>Test 2</li>
<li #click = "selectComponent('appBugs3', $event)"><i class="ion-bug"></i>Test 3</li>
Add the following method:
selectComponent: function(component, event){
if(this.activeLink){
this.activeLink.classList.remove('highlight');
}
this.activeLink = event.target;
this.activeLink.classList.add('highlight');
this.selectedComponent = component;
};
And the property activeLink. Then add your css styling, for example:
.highlight{
background-color: yellow;
}

Is it possible to set class of an item after it has been declared?

I would like to have all my menu items which are common to all the pages on my website in a single header file. The problem lies that I need to define a class for the current menu item so it changes color. I`m using superfish for the menu, here is a very simple mockup...
<nav>
<ul class="sf-menu">
<li id="first-li">Home</li>
<li class="current">Page 1
<ul>
<li>Submenu 1 </li>
<li>Submenu 2
<ul>
<li>SUBSubmenu 1 </li>
</ul>
</li>
</ul>
</li>
<li>Page 2 </li>
<li>Page 3</li>
<li>Page 4</li>
</ul>
</nav>
Is it possible to remove class="current" , move the whole nav to the header file and then only specify the current class on each page?
My actual menu code is much much larger and this is why I do not want it repeated on every page.
use this in your current page script
Use jquery for the thing..
$('.sf-menu li a').click(function() {
$('.sf-menu li.current').removeClass('current');
$(this).closest('li').addClass('current');
});
find the fiddle here..http://jsfiddle.net/VudYx/
Add class="current" to the index.php li in the header and just use the following jQuery code in header.
$(document).ready(function() {
var curUrl = window.location.pathname;
curUrl=curUrl.replace(/\/$/, "");//support urls with or without trailing slash
$( ".sf-menu li" ).each(function(i) {
if(($(this).children().length > 0) && ($(this).children(":first").attr("href").split('/').pop()==curUrl.split('/').pop()))
{
$( ".sf-menu li" ).removeClass("current");
$(this).addClass("current");
}
});
})
I got it working exactly how I want by combining a little of all the answers.
First I gave id`s to all the 1st tier menu items, the ones I want highlighted, then on each page I move the current class like so...
<script>
$(document).ready(function() {
$( ".sf-menu li" ).removeClass("current");
$( "#menu2" ).addClass("current");
});
</script>
clean, simple and works without clicking, thanks to all that answered you all helped.
Personally I'd achieve this using CSS.
If each menu item <li> has its own class, and your <body> has a unique ID per page, you can style the current item appropriately such that:
<li class="navHome">Home</li>
<li class="navAbout">About<li>
and your home/about pages have a body tag such that:
<body id="pgHome"> or <body id="pgAbout">
Then you can directly style the current page's menu item:
#pgHome .navHome,
#pgAbout .navAbout
{
// styles to highlight this item
}

keeping highlighted tab in navigation bars

thanks for your time.
I've been having a lot of trouble trying to highlighting the "active" tab on a navbar i am using. I'm trying to do this through CSS but the problem arises when I change pages. I will add the following code:
function updateMenu(num)
{
var menuCode =
'<ul id="menu">' +
'<li><a href="software/menu.php" onclick="updateMenu(1);"';
if(num == 1){menuCode +=' class="current"';}
menuCode += '>Software</a></li>'+
'<li><a href="users/menu.php" onclick="updateMenu(2);"';
if(num == 2){menuCode +=' class="current"';}
menuCode += '>Software</a></li>';
document.getElementById("cssMenu").innerHTML = menuCode;
}
And my list goes as follows:
<ul id="menu">
<li>Software</li>
<li>Users</li>
</ul>
I feel it's an unelegant solution because of all the code wrote in the updateMenu function and i was wondering if there was a more elegant solution to my problem. (You can see it's on moving the "class=current" so the CSS works properly).
I'm not sure what your exact requirement is. Assuming that on clicking the tab, it does NOT go to another page, the following code will help [please use Jquery]:
HTML :
<ul id="menu">
<li>item 1</li>
<li>item 2</li>
<li>item 3</li>
</ul>
Javascript :
<script>
$(document).ready(function(){
$("#menu li").click(function(){
$("#menu li").removeClass("highlight");
$(this).addClass("highlight");
});
});
</script>
CSS :
.highlight {
background: #f00;
}

jQuery Deep linking, load content from div in other page issues

I was able to set up a function where upon clicking the nav link it loads the "#content" div from the appropriate page into the "#content" div on the current page. The issue arose when I tried implementing deep linking with the address plugin. I can't seem to figure out how to get it to load just the "#content" div from a page.
You can see it live here: www.theeastcoastclassic.com/index1.html
The bottom nav still has the original .load function, the top is using the deep linking function.
Here's my .js file:
// Deep Linking
function loadURL(url) {
console.log("loadURL: " + url);
$("#content").load(url);
}
$.address.init(function(event) {
console.log("init: " + $('[rel=address:' + event.value + ']').attr('href'));
}).change(function(event) {
$("#content").load($('[rel=address:' + event.value + ']').attr('href'));
console.log("change");
})
$('ul#topNav a').live('click', function(e){
$.scrollTo('#content', 'slow');
$("#content").hide();
loadURL($(this).attr('href').fadeIn("6000"));
e.preventDefault();
});
// Top Nav Hijax
/* $("ul#topNav a").live("click",function(e) {
$.scrollTo('#content', 'slow');
var url = $(this).attr("href") + " #content";
$("#content").hide().load(url).fadeIn("6000");
e.preventDefault();
}); */
// Bot Nav Hijax
$("ul.bNav a").live("click",function(e) {
$.scrollTo('#content', 'slow');
var url = $(this).attr("href") + " #content";
$("#content").hide().load(url).fadeIn("6000");
e.preventDefault();
});
//Equal Height Columns
$(document).ready(function() {
$("div .col3").equalHeights();
});
This is the abridged HTML:
<ul id="topNav">
<li>
Home
</li>
<li>
Schedule
</li>
<li>
Lodging
</li>
<li>
Sponsors
</li>
<li>
Directions
</li>
<li>
Contact
</li>
</ul>
<div id="content" class="clearfix">
</div>
Also if you have any tips about the equalHeights columns please let me know, it seems as though this plugin is very simple but appears differently on each browser, I'm getting a lot of scrollbars.
You need to add a change() function, so if the address changes this will be executed:
$.address.change(function(event) {
// your loading logic goes here
});