How to exclude menu link from jQuery click handler? - javascript

My project is using a simple toggle menu and I need to exclude the last link (#blog) from the click handler doing scroll animations and URL modifications.
Here is HTML:
<nav class="navbar">
<span class="navbar-toggle" id="js-navbar-toggle">
<i class="fas fa-bars"></i>
</span>
<ul class="main-nav" id="js-menu">
<li>
Home
</li>
<li>
About
</li>
<li>
Skillset
</li>
<li>
Portfolio
</li>
<li>
Contact
</li>
<li>
<a id="blog" href="#https://anzudev.blogspot.com/" target="_blank" class="nav-links">Blog</a>
</li>
</ul>
</nav>
And here jQuery:
$('nav a[href*="#"]').not("#blog").on("click", function () {
$("html, body").animate({scrollTop: $($(this).attr("href")).offset().top }, 1000);
});
I've added a jQuery click handler to all nav-links to smoothly scroll to that section and modify the URL accordingly. However, I need to exclude the #blog nav-link, since that redirects to an external site. I've tried the following selectors:
- $('nav a[href*="#"]').not("#blog")
- $('nav a[href*="#"]').not(":last")
But upon clicking "#blog" will still attempt to go to myurl.com/#https://anzudev.blogspot.com/ instead of an external link.
I have a simple mockup of my code here on Codepen for testing.
Any advice on what I am doing wrong here? Cheers!

You just have to delete the # from the beginning of the blog link URL:
https://anzudev.blogspot.com/
instead of
#https://anzudev.blogspot.com/
Anything starting with # will be attached to the end of current page URL.
Working edited CodePen: https://codepen.io/liquidmetalrob/pen/gObzvOg?editors=1010

Your href link on your blog should just be:
"https://anzudev.blogspot.com/"
and not
"#https://anzudev.blogspot.com/"

Related

Close off-canvas mobile menu when link is clicked (JS)

I have a mobile menu that has some anchored links that scroll to a certain section of the page when clicked. The problem with this is when I click one of these links, due to the fact that a new page is not loading, the menu remains open.
I wish to implement a solution whereby if one of these links are clicked, the menu closes by default. I have tried to hide the menu on click using JS but I think my logic may be wrong. Any help would be great.
Here is the code for my menu.
<div id="my-id" class="uk-offcanvas custom_canvas">
<div class="uk-offcanvas-bar">
<div class="uk-panel">
<a class="btn btn primary remove_image-div" onclick="UIkit.offcanvas.hide([force = false]);">
<img src="img/remove_icon.png" class="remove_image" alt="remove">
</a>
<ul class="mm-list mm-panel mm-opened mm-subopened" id="mm-0">
<li class="offcanvas_li">
<a class="offcanvas_open" title="Samples" href="index.html#sample-section">Samples</a>
</li>
<li class="offcanvas_li">
Packages
</li>
<li class="offcanvas_li">
About
</li>
<li class="offcanvas_li">
Contact
</li>
<li class="offcanvas_li">
Blog
</li>
<li class="offcanvas_li">
<a class="offcanvas_open" title="We Love" href="we_love.html">We Love</a>
</li>
</ul>
</div>
</div>
</div>
I'm not familiar with UIkit.offcanvas plugin but with a little sense I can answer you that you can add them (the links) all the attribute onclick="UIkit.offcanvas.hide([force = false]);"
A better (generic) solution is to "listen" to their click event, then, run offcanvas command.
Something like:
$('.offcanvas_li a').click(function() {
UIkit.offcanvas.hide([force = false]);
});
This works
<script>
function myFunction() {
UIkit.offcanvas.hide([force = false])
}</script>
<button onclick="myFunction()">Close Panel</button>

<a href=#> link redirecting to index. php?

<div class="filter-portfolio pull-right">
<ul>
<li class="always-visible">
****Categories <span class="icon-caret-down"></span>
</li>
<li>
All
</li>
<li>
Fashion
</li>
<li>
Nature
</li>
<li>
Animals
</li>
<li>
Architecture
</li>
</ul>
</div>`
Whenever I click on the **Categories link it is redirecting me to the index.php/#. Can anyone help me find out why it's happening ?
Prevent default click handler:
$('.filter-portfolio a[href="#"]').on('click', function(event) {
event.preventDefault();
});
The # sign scrolls the page to the top when the anchor is clicked, and adds the hash sign to the URL, if you don't want that, just prevent it
$('a[href="#"]').on('click', function(e) {
e.preventDefault()
});
You could also omit the href attribute like this and you would not need any javascript for it.
Like this:
<a data-filter=".all">All</a>

Getting jquery and css transition effects working together

I have cloned this template :
http://ironsummitmedia.github.io/startbootstrap-grayscale/
When you scroll-down, notice that the background-color of the header-link(About, Download, contact) on top will change to gray color (depending upon the section where you have scrolled).
I have made some small changes to the header wherien I am including font-awesome icons. The color effect has however stopped working.
<ul class="nav navbar-nav nav-advance-options hide-this-nav">
<!-- Hidden li included to remove active class from about link when scrolled up past about section -->
<li class="hidden">
</li>
<li>
<a class="page-scroll" href="#flax">
<i class="fa fa-leaf"></i>
</a>
</li>
<li>
<a class="page-scroll" href="#moist">
<i class="fa fa-heart"></i>
</a>
</li>
<li>
<a class="page-scroll" href="#designs">
<i class="fa fa-paw"></i>
</a>
</li>
</ul>
http://plnkr.co/edit/0MV3QzRpJcPWlpuNMDav
I can't tell exactly where your javascript has gone wrong by the assets you've provided, but the issue is that on the font awesome icon version of the site (http://plnkr.co/edit/0MV3QzRpJcPWlpuNMDav), the active class is not being added to the menu li elements upon scroll, whereas it is being added to the li elements on the demo site (http://ironsummitmedia.github.io/startbootstrap-grayscale/).
solved the issue by removing the following lines from second ul:
<li class="hidden">
</li>
I don't know why this solves the issue yet. If someone understands, kindly explain

Change class depending of the page. jQuery

I'm trying to add an attribute "selected" to a button. It's a group of 5 buttons but I just want one of them to be "selected" according to the page title.
This is what I have: http://jsfiddle.net/M6Ypu/
<ul id="menu">
<li>
<a id="Home" class="selected" href="#">Home</a>
</li>
<li>
<a id="categories" class href="#">Categories</a>
</li>
<li>
<a id="franchise" class href="#">Franchise</a>
</li>
<li>
<a id="about" class href="#">About</a>
</li>
<li>
<a id="contact" class href="#">Contact</a>
</li>
</ul>
Those are in a Master Page for ASP.NET and I want to change the class of the buttons so if I am at categories.aspx the class for a#categories is changed to "selected".
Hope you can help me.
Try something like this:
$(document).ready(function(){
$("ul#menu a").click(function(){
$("ul#menu a").each(function(){
$(this).removeClass('selected');
});
$(this).toggleClass('selected');
});
});
Working fiddle: http://jsfiddle.net/robertrozas/M6Ypu/3/
I could make it work by adding a script to the pages using the Master Page
Home page has this code and the selector changes depending on the page I am:
<script>
$(document).ready(function () {
$('a.selected').removeClass();
$('a#Home').addClass("selected");
});
</script>
Since there were only 5 pages, it was much easier to just add the script to each one of them.

At first redirect to url and then change href

This is my code I want, when I click to link at first redirect to page by link and then change href. The problem is when I click this href change but page don't change
$(window).load(function() {
$("#product").click(function() {
$('#product').attr('href', 'javascript:void(0);');
$('#seller').attr('href', 'http://www.vmarket.com.vn/products.php?os=offer');
$('#buy').attr('href', 'http://www.vmarket.com.vn/products.php?os=seeking');
});
$("#seller").click(function() {
$('#product').attr('href', 'http://www.vmarket.com.vn/products.php');
$('#seller').attr('href', 'javascript:void(0);');
$('#buy').attr('href', 'http://www.vmarket.com.vn/products.php?os=seeking');
});
$("#buy").click(function() {
$('#product').attr('href', 'http://www.vmarket.com.vn/products.php');
$('#seller').attr('href', 'http://www.vmarket.com.vn/products.php?os=offer');
$('#buy').attr('href', 'javascript:void(0);');
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="searchTabs">
<li class="current">
<a rel="nofollow" href="products.php" id="product">Sản phẩm</a>
</li>
<li class="tabRight">
<a rel="nofollow" href="products.php?os=offer" id="seller">Cung</a>
</li>
<li>
<a rel="nofollow" href="products.php?os=seeking" id="buy">Cầu</a>
</li>
</ul>
Why do you need to change the href attribute for the link? Why don't you just redirect them straight away using:
window.location.href='/to/infinity/and/beyond';
I think i understand what you're trying to do.
The reason why it does not work is because when you click the links it will then as normal send the user to whatever that's inside the href. Looks like what you're looking for is a method of having the links change dynamically depending on which page you're on? Right?
I would suggest using a server side language to handle that. Using parameters to control what link to show.
For an example:
<ul id="searchTabs">
<li class="current" >
<a rel="nofollow" href="products.php" id="product">Sản phẩm</a>
</li>
<li class="tabRight">
<a rel="nofollow" href="products.php?os=offer" id="seller">Cung</a>
</li>
<li >
<a rel="nofollow" href="products.php?os=seeking" id="buy">Cầu</a>
</li>
</ul>
Would become:
<ul id="searchTabs">
<li class="current" >
<a rel="nofollow" href="products.php?links=1" id="product">Sản phẩm</a>
</li>
<li class="tabRight">
<a rel="nofollow" href="products.php?links=2" id="seller">Cung</a>
</li>
<li >
<a rel="nofollow" href="products.php?links=3" id="buy">Cầu</a>
</li>
</ul>
Then you could with a server side language such as PHP do a switch on the link parameter. Then depending on what is set change the href attributes.
-
Reason why yours isn't working is because it redirect the user. Which means you're JavaScript won't matter at all as none of it have any influence on the other page.
You could prevent it from changing the actual location but it does not sound like that's what you're looking for. But just in case.. In order to prevent such event you can use:
$(window).load(function(){
$("#product").click(function(e){
e.preventDefault();
$('#product').attr('href','javascript:void(0);');
$('#seller').attr('href','http://www.vmarket.com.vn/products.php?os=offer');
$('#buy').attr('href','http://www.vmarket.com.vn/products.php?os=seeking');
});
$("#seller").click(function(e) {
e.preventDefault();
$('#product').attr('href','http://www.vmarket.com.vn/products.php');
$('#seller').attr('href','javascript:void(0);');
$('#buy').attr('href','http://www.vmarket.com.vn/products.php?os=seeking');
});
$("#buy").click(function() {
e.preventDefault();
$('#product').attr('href','http://www.vmarket.com.vn/products.php');
$('#seller').attr('href','http://www.vmarket.com.vn/products.php?os=offer');
$('#buy').attr('href','javascript:void(0);');
});
});
Hope that helps :) Leave a comment if there is anything you would like to explained.

Categories