Show Active Nav Link with JavaScript

  Рет қаралды 18,016

Coding in Public

Coding in Public

Күн бұрын

In this video, I'll show you how to show which page is the active webpage to set it apart visually.
🔗 Key Links 🔗
- CodePen: codepen.io/Cod...
- MDN docs on aria-current: developer.mozi...
---------------------------------------
📹 Related Videos 📹
- Responsive Navbar: • Responsive Navbar HTML...
---------------------------------------
🌐 Connect With Me 🌐
- Website: codinginpublic...
- Blog: chrispenningto...
- Twitter: / cpenned

Пікірлер: 84
@Marty72
@Marty72 2 жыл бұрын
Just a heads up, the text-decoration shorthand notation doesn't work in Safari.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
I didn’t realize that. Thanks so much!
@DragonRoyZ
@DragonRoyZ Жыл бұрын
Of all the Tutorials, this is the most straight forward and short solution. Thanks a lot!
@CodinginPublic
@CodinginPublic Жыл бұрын
So glad it was a help!
@johnmarkbondad6009
@johnmarkbondad6009 Жыл бұрын
I never thought this would be done in a simpler way. thanks
@CodinginPublic
@CodinginPublic Жыл бұрын
You’re welcome!
@AlThePal78
@AlThePal78 Жыл бұрын
Literally made my life easier I am trying to do my own stuff because bs got confusing after a while when I was using it so I want to do my own thing now thanks for the insperation
@CodinginPublic
@CodinginPublic Жыл бұрын
Glad it was a help!
@Fatima-ie5kj
@Fatima-ie5kj 2 жыл бұрын
I always make a custom class active and switching between this class manually between page's but not anymore after watching this.🥰 Thank you
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Same! I've always done that until I just found this a few weeks back!
@MarshallSC1
@MarshallSC1 2 жыл бұрын
And, you just elevated what already was a high quality content of tutorials. Aria labels are important but often neglected. P.S. Video description is from one of the previous videos.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Yes, been trying to learn aria labels myself and hoping passing on that knowledge is a help! (Thanks for the heads up on the video description! 😬)
@anonymouseverx
@anonymouseverx 2 жыл бұрын
Extremely helpful with new solutions like always. Thank you so much, Chris.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
So glad you enjoyed it!
@adnanamin3666
@adnanamin3666 2 жыл бұрын
Very clear explanation in a short amount of time! Clever! Thanks a lot.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
You’re welcome! Glad it was a help!
@neontuts5637
@neontuts5637 2 жыл бұрын
Thanks for sharing this amazing way to add the current page indicator. It's easy to implement and adds accessibility.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Glad it was a help! Just found about about the aria-current attribute myself and stopped using "active" classes everywhere :)
@neontuts5637
@neontuts5637 2 жыл бұрын
​@@CodinginPublic Awesome! Thanks again 🙏
@humbulanimunyai6320
@humbulanimunyai6320 2 жыл бұрын
As always, clear step by step explanation, anyway i enjoyed that part of window object
@CodinginPublic
@CodinginPublic 2 жыл бұрын
🙏
@lukecartwright613
@lukecartwright613 2 жыл бұрын
This is awesome! It's both accessible AND cool. Thank you
@CodinginPublic
@CodinginPublic 2 жыл бұрын
You’re welcome! Glad you enjoyed it!
@ClarkeDesign
@ClarkeDesign 2 жыл бұрын
Nice tutorial. For tutorials though, can you use a more easy to read font (for class and href for example) for some of your elements in the code. Just makes it easier to digest.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Thanks for the suggestion. I just need to set up a different workspace for recording videos. I don't want to make things harder on people to watch, but I also don't want to have to switch up multiple settings every time I go to record. I'll work on it. Thanks for the suggestion!
@astroboy3720
@astroboy3720 2 жыл бұрын
I needed this so much, thank you for this clear vid.
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Sure thing! Glad it was a help. Thanks for saying something!
@eatatjoe69
@eatatjoe69 Жыл бұрын
this was pretty good. Informative and i think I learned JS
@CodinginPublic
@CodinginPublic Жыл бұрын
Great!
@charanathenuka3423
@charanathenuka3423 Жыл бұрын
excellent work! thank you very much❤
@CodinginPublic
@CodinginPublic Жыл бұрын
You’re welcome! So glad it was a help!
@kevonevans3107
@kevonevans3107 Жыл бұрын
Thanks for this works perfectly
@CodinginPublic
@CodinginPublic Жыл бұрын
You’re welcome!
@lhc2721
@lhc2721 Жыл бұрын
thanks, this was to the point and nicely explained. :)
@CodinginPublic
@CodinginPublic Жыл бұрын
Glad it was helpful!
@absobel
@absobel Жыл бұрын
Very helpful, thanks
@CodinginPublic
@CodinginPublic Жыл бұрын
Glad you found it helpful!
@c4346
@c4346 2 жыл бұрын
Very clever, thanks for the tip!
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Sure thing!
@devlife5596
@devlife5596 2 жыл бұрын
I need to sign in for comment. Firstly, Thanks for your TUT. Second. Can you please DO NOT change VS Font Style. Its very hard to read. Its extremely waste time visitors time for reading your code. Any way, I still like this videos. Thanks for your time!
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Glad you’re enjoying the channel! And I appreciate your opinion on the font.
@twinconceptsdesign
@twinconceptsdesign 2 жыл бұрын
Really great 👍 👌
@hamzanouh9153
@hamzanouh9153 Жыл бұрын
Great and easy way
@CodinginPublic
@CodinginPublic Жыл бұрын
Thank you so much 😊
@amjadnikzad4890
@amjadnikzad4890 2 жыл бұрын
thank you it was informative!
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Great! Thanks for saying something!
@pc8406
@pc8406 Жыл бұрын
thank you😊
@CodinginPublic
@CodinginPublic Жыл бұрын
You're welcome 😊
@samnorte1891
@samnorte1891 10 ай бұрын
Fantastic! Thanks for the tutorial, but it looks like this is not working for dropdown menu, isn't it?
@CodinginPublic
@CodinginPublic 10 ай бұрын
Hmm. I don’t remember? Been too long since I built this…it should work the same though unless I overwrote the styles somehow here?
@lvekua
@lvekua 2 жыл бұрын
Love it! ❤
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Thanks for saying something!
@lvekua
@lvekua 2 жыл бұрын
@@CodinginPublic actually I wanted to ask you if this method could work with navigation that links to other sections with ids on the same page.
@blackpurple9163
@blackpurple9163 Жыл бұрын
Great explanation, btw any way to animate that wavy underline so it looks like it's moving? With css only if possible otherwise any other way? I'm using react+tailwind so I don't want to depend on any other libraries or frameworks if possible
@CodinginPublic
@CodinginPublic Жыл бұрын
Glad you enjoyed it! There's not an easy way to do it with the underline property, but you could do something like this with an ::after pseudo-element? You'd likely have to create a custom keyframe animation, but it's possible. Not easy to explain in a comment though :)
@blackpurple9163
@blackpurple9163 Жыл бұрын
@@CodinginPublic I understand, I thought of doing that with an svg in the after pseudo-class but since I'm very new to svg, I don't know how to make it 'wavey' so it doesn't look like 2 images changing from one to the other
@VectorBiH
@VectorBiH Жыл бұрын
thank you
@CodinginPublic
@CodinginPublic Жыл бұрын
You’re welcome!
@Strawberry00562
@Strawberry00562 Жыл бұрын
thanks bro
@CodinginPublic
@CodinginPublic Жыл бұрын
You're welcome!
@axvgarcia
@axvgarcia Жыл бұрын
thanks for the video! one question, how can I keep the aria-current attribute in a navigation link after selecting a child page of that link?
@CodinginPublic
@CodinginPublic Жыл бұрын
Well, because the aria attribute says something about the actual page you’re on, you’d always want it to reflect the actual page. If you’re just going for a visual styling for the parent of the sub page, you could add some regex or something to check the current url against your parent pages and add a class of “active” or something to those parents?
@figureof8795
@figureof8795 2 жыл бұрын
For some reason my console is not showing the links. Ive correctly linked my external JS file but the following code does'nt seem to work: document.querySelectorAll('.nav-link').forEach (link => { console.log(link); }) EDIT: does the file have to be html or can it be php?
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Yeah, PHP would be different. Sorry I can't help with PHP.
@figureof8795
@figureof8795 2 жыл бұрын
@@CodinginPublic no problem. Thanks for the response.
@tomasslezak3628
@tomasslezak3628 2 жыл бұрын
Nice trick
@tulioferreira8766
@tulioferreira8766 Жыл бұрын
ajudou muito, thank you parça
@CodinginPublic
@CodinginPublic Жыл бұрын
You’re welcome!
@luckyrao6271
@luckyrao6271 Жыл бұрын
how to use same navbar in all webpages of the website and also this trick to show active nav link
@CodinginPublic
@CodinginPublic Жыл бұрын
You need to use some kind of framework that allows you to build components (Astro, Hugo, etc.) and then use that component on each page. Hope that helps!
@icwhy4366
@icwhy4366 Жыл бұрын
i'm pretty sure you can use sections to do this
@icwhy4366
@icwhy4366 Жыл бұрын
its possible using java jquery I just tested it.
@sydneysolomon5500
@sydneysolomon5500 Жыл бұрын
What software did u use for your coding?
@CodinginPublic
@CodinginPublic Жыл бұрын
VSCode :)
@sairohit5510
@sairohit5510 2 жыл бұрын
how to do this with scroll snap?
@CodinginPublic
@CodinginPublic 2 жыл бұрын
Here’s a video I did on scroll snap. Hopefully this helps? Add Scroll Snap with CSS kzbin.info/www/bejne/jKavq4GalrV1obM
@marwanbaz5885
@marwanbaz5885 Жыл бұрын
how to use this in a react website?
@CodinginPublic
@CodinginPublic Жыл бұрын
If you use React Router, the NavLink component sets this by default. So you can just style it with CSS.
@izzatrahman451
@izzatrahman451 Жыл бұрын
good video but I am stuck at document undefined :(
@CodinginPublic
@CodinginPublic Жыл бұрын
Did you link to your JavaScript document in your HTML page?
@izzatrahman451
@izzatrahman451 Жыл бұрын
@@CodinginPublic ya, i put my nav in the layout might be the issue?
@CodinginPublic
@CodinginPublic Жыл бұрын
There are a few ways to this this, but move it to the bottom of your document just before the closing body tag and see if that helps :)
@nevendrean5772
@nevendrean5772 Жыл бұрын
You may need to "link.removeAttribute('aria-current')" before
@CodinginPublic
@CodinginPublic Жыл бұрын
Perhaps on a SPA? But if you’re changing routes, any of those manually-added attributes will clear.
How to create a Responsive Navigation Bar (for beginners)
15:21
Coding2GO
Рет қаралды 403 М.
Apple peeling hack @scottsreality
00:37
_vector_
Рет қаралды 125 МЛН
Пришёл к другу на ночёвку 😂
01:00
Cadrol&Fatich
Рет қаралды 9 МЛН
Шок. Никокадо Авокадо похудел на 110 кг
00:44
ASMR Programming - Weather App With Javascript - No Talking
20:30
AsmrProg
Рет қаралды 2,7 МЛН
Easily change sticky nav styling on scroll
12:12
Kevin Powell
Рет қаралды 66 М.
My new favorite way to use git
5:29
Coding in Public
Рет қаралды 6 М.
21 Awesome Web Features you’re not using yet
8:31
Fireship
Рет қаралды 978 М.
Every modern site should be using PostCSS!
18:58
Coding in Public
Рет қаралды 14 М.
The Parallax Effect // 5 Minute WebDev Project
5:01
Fireship
Рет қаралды 622 М.
Navbar CSS Tutorial: 3 Ways to Create a Navigation Bar with Flexbox
15:08
Before/After Image Slider Comparison (HTML, CSS, and JavaScript)
16:45
Coding in Public
Рет қаралды 22 М.
Add and Remove Active Class on Click
5:20
ByteGrad
Рет қаралды 40 М.
The Biggest Mistake Beginners Make When Web Scraping
10:21
John Watson Rooney
Рет қаралды 113 М.
Apple peeling hack @scottsreality
00:37
_vector_
Рет қаралды 125 МЛН