/    Sign up×
Community /Pin to ProfileBookmark

Modal Close – Jumping to Top of Page

First off … I’m primarily a designer, and not a programmer, so be gentle and use small words when providing help 😀

I have a css modal window. When I click the link, a modal window opens. When I close the link, the window closes, and the page jumps all the way to the top of the page. I need the page to stay where it is when the modal window is closed.

After doing some searching, I know this is because of the anchor tag being called in the href close link:

`<a class=”modal-close” href=”#” onclick=”modal_close(‘ModalVid’);”>&times;</a>
`

I posted the modal code on codepen… https://codepen.io/jabbamonkey/pen/JjPMmKv

**WHAT I’VE TRIED**
After some online searching, I saw recommendations to add the preventDefault() function OR “return false”. So, I tried to add it to the onclick close function … but neither worked. I may not be using the two correctly (my knowledge of javascript is pretty limited)

PREVENTDEFAULT()

“`
function modal_close(id) {
var ModalVideo = document.getElementById(id);
ModalVideo.pause();
preventDefault();
}
“`

RETURN FALSE

“`
function modal_close(id) {
var ModalVideo = document.getElementById(id);
ModalVideo.pause();
return false
}
“`

Other recommendations said to try to add “overflow: hidden;” to the body, but I think that recommendation only works for jquery bootstrap modals.

Please help. Thanks…

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@codyhillSep 17.2019 — @jabbamonkey#1608841 Fixed it!

https://codepen.io/raul-rogojan/pen/eYOLPjo?editors=1000

**_The problem:_**

You see, every time you have an a tag with href="#". that link button will ALLWAYS make the jump to the top of the page. If you add an ID then it will jump to the specific id. [Here](https://codepen.io/raul-rogojan/pen/OJLoBdz?editors=0010) I created an example for you.

All I did to fix it is, I went to the modal-close and change the a tag hfre from # to #234. An ID that does not exist in your file. I literally just pressed random buttons and I ended up with 234...

**_Sugestion_**

Do not use a tag for such things. Use a tag only for buttons that send you to another page or scroll to a specific location on your page. An example would be the navbar. Try using &lt;button&gt;&lt;/button&gt; or even div. Also, I am not sure how popular is onclick events in HTML nowadays. Try using const closeButton = document.querySelector('.modal-close').
Copy linkTweet thisAlerts:
@codyhillSep 17.2019 — @jabbamonkey#1608841 Oh, and overflow: hidden on the body is a valid solution. But it's for a different problem. That will stop the background from scrolling if you scroll while you inside the modal. But also you have to remove it when you close the modal. And no, no it's not only for jquery bootstrap modals. :)
Copy linkTweet thisAlerts:
@jabbamonkySep 17.2019 — Wow... that was an easy fix. Makes me even MORE FRUSTRATED with MYSELF for not figuring it out. Hahahaha. Thanks alot.
Copy linkTweet thisAlerts:
@codyhillSep 17.2019 — @jabbamonky#1608844 HAHAH, been there. I still am. No need to be frustrated. It happened to everyone. You can check some of my old posts in here hahaha.
×

Success!

Help @jabbamonkey spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...