JQuery - Show / Hide - the page always rises by click

I am using the show / hide jQuery function in a form (generated by php)

There are 10 hidden elements in this form, and each time the user clicks the “add another” link, he displays the next hidden element, and my footer drops with it. So far so good.

Now the problem is that 2 or 3 hidden elements are shown, the page gets larger, and the right navigation scrollbar from firefox appears, which is good, but then when I click on the next link “add another”, the navigation scrollbar returns to the beginning page while a hidden item is displayed. Every link that adds a new hidden element takes the page up, and I don't want this behavior ...

Does anyone know how to fix this?

+3
source share
2 answers

How about adding false at the end of the click event

+3
source

Is the “add another” actual item awith a click handler that adds new content?

If so, you probably want to event.preventDefault(). Try something like this:

$('.myClickedLink').click(function(event) {

    event.preventDefault();
    // Run my code

})

This prevents the default link behavior. I guess this is what makes your page return to the top every time.

EDIT: First, in my code, I mistakenly typed stopPropagation () instead of preventDefault (). This was correct in the text above. Unfortunately.

+5
source

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


All Articles