Javascript help, toggle text keeps jumping back to the top of the page

Hey guys, I hope someone can help with this problem, I tried a lot of scripts and workarounds, but can't get this simple job to work. As the headline has it, how can I make my text NOT go to the top of the page. here is the code.

<script type="text/javascript"> function toggleVisibility() { document.getElementById("toggleMe").style.display = ""; if(document.getElementById("toggleMe").style.visibility == "hidden" ) { document.getElementById("toggleMe").style.visibility = "visible"; } else { document.getElementById("toggleMe").style.visibility = "hidden"; ev.preventDefault(); return false; } } </script> <a href="#" onclick="toggleVisibility();">Click to toggle visibility.</a></p> <div id="toggleMe" style="visibility: hidden;"> Something to Hide and show. 

The display collapses the layout, and visibility saves it.

all in the same html file. I read what the outters did, and I tried ev.preventDefault (); return false; code at the end of the function, but nothing.

Thanks!

+4
source share
2 answers

Change the href attribute in tag A to javascript:; instead of #

So use this to create a javascript-enabled anchor:

 <a href="javascript:;"> 

added (reference information):

The # symbol indicates a bookmark. Omitting the bookmark name, for example. #bottom browser considers the bookmark β€œtop of the page” by scrolling up. Your problem shouldn't do anything with bookmarks, however using # to define an empty link for javascript anchor tags tagged to anchor ( A ) is not the way to go. You should use: javascript:; or javascript:void(0); to indicate that this is not a navigation binding.

+7
source

Or just always return false, not just the Else clause.

0
source

Source: https://habr.com/ru/post/1339820/


All Articles