Skip to content

Some tips & tricks and snippets for javascript

License

Notifications You must be signed in to change notification settings

cavo789/javascript_tips

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

javascript_tips

Banner

Some tips & tricks and snippets for javascript.

Snippets

Change color of links when clicked

When a link has been clicked, change his style to make simple to visualize un visited links.

Prefer js and not css (a:visited {color: cyan; }) so we can simply refresh the page (JS) without having to clear the browser history (css).

Idea is to be able to detect on a page which links have been clicked.

When a link will be clicked, the visited class will be added to highlight the link. When a second link will be clicked, all links with class visited will be modified to already_visited so we can make a clear, visual, difference between the last clicked one and the old ones.

Visited links

<style>
    .visited { background-color: #ffc107; }
    .already_visited { color: #f3e3e3 !important; }
</style>
<script>
document.addEventListener("DOMContentLoaded", () => {
    (
        addEventListener('click', function (ev) {

            for(var i = 0, l=document.links.length; i<l; i++) {
                if (document.links[i].classList.contains('visited')) {
                    document.links[i].classList.remove('visited');
                    document.links[i].classList.add('already_visited');
                }
            }

            ev.target.classList.add('visited');
        })
    )
});
</script>

Author

Christophe Avonture

License

MIT

About

Some tips & tricks and snippets for javascript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages