Auto scroll when Text appears | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Auto scroll when Text appears

I'm trying to put multiple buttons next to each-other. When I press on a button, the text appears and auto-scroll happens. This works. But when I want more buttons, then it doesn't work the way I want it to work. Either it shows an error and neither buttons work, or the second button shows the same text as the 1st button. This is the 1st button which I'm trying to copy. <button id="scroll" class="button-friends-clickon" type='button' onclick="toggleText()"><a href="#scroll" style="color:yellow">Demo</a></button> <p id='friends-filia' style='display:none'>text</p> function toggleText() { var text = document.getElementById("friends-filia"); if (text.style.display === "none") { text.style.display = "block"; } else { text.style.display = "none"; } } For the second Button, I'm using the same code but with different ID's and text. Anyone know what to do? Or maybe another code would solve this? Thank you for any help!

23rd Feb 2023, 3:34 PM
Haris
3 Answers
+ 3
It worked when I tried with adding a parameter for the text ID to the function, pass a different text ID from each onclick and make each href references different ID for scrolling.
23rd Feb 2023, 5:05 PM
Lochard
Lochard - avatar
+ 2
If you just want to scroll to the bottom, you can use this: let pageX = window.pageXOffset; window.scrollTo(pageX, document.body.scrollHeight); Here are more informations about scrolling. https://javascript.info/size-and-scroll-window
23rd Feb 2023, 5:30 PM
Stefanoo
Stefanoo - avatar
23rd Feb 2023, 6:05 PM
JaScript
JaScript - avatar