Skip to content
Advertisement

Why do I have to click my event handler target twice before the DOM is updated?

I am making a time tracking dashboard site, it has 3 links (Daily, Weekly, and Monthly) and I want the content to change based on which link is clicked. I am setting hash values for all 3 links as #daily, #weekly, and #monthly and in my jQuery script I am hiding and showing divs by adding if conditions that uses location.hash property to hide and show specific. The script is working somewhat but the content changes only when I double click on a link, I don’t know what is wrong with my script. I have made a repository and also enabled GitHub pages for it, GitHub Repository and GitHub Pages Link.

JavaScript
JavaScript
JavaScript

Advertisement

Answer

The problem really is just a race condition basically… You check the location hash before it gets updated by the click on the a. But, is that really what you’d like to do? Change the display according to the hash value, on a user click? Wouldn’t you be better to just listen to hash changes, and act accordingly?

JavaScript

Every time the hash changes, hideAndShow will trigger and do the changes needed… That way you do not have to remember to add X or Y events on every click that changes the hash value.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement