Solved

Create "Go to top" Button in K2 SmartForm

  • 15 January 2024
  • 2 replies
  • 58 views

Badge +2

Hi everyone,

I want to create a “Go to top” Button in my Smartform, I have put this script into a DataLabel Extension: 

<body>

<button onclick="topFunction()" id="myBtn" title="Go to top">Top</button>

<script>

let mybutton = document.getElementById("myBtn");

window.onscroll = function() {scrollFunction()};

function scrollFunction() {

  if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {

    mybutton.style.display = "block";

  } else {

    mybutton.style.display = "none";

  }

}

function topFunction() {

  document.body.scrollTop = 0;

  document.documentElement.scrollTop = 0;

}

</script>

</body>

But it did not work.

Please help me to solve this, thank you!

icon

Best answer by Deon 16 January 2024, 01:02

View original

2 replies

Badge +8

Hi @HungTran , I tried your code and it works fine for me.

I did however do it by adding your code (without the <body> tags, because it is already inside a body) to an Expression and setting the Expression Property of the data label.

Please ensure that Literal is checked and that Prevent XSS is unchecked:

 

 

In this example I also achieved the same with a normal button control and by just doing a transfer data with this value: “<script>document.documentElement.scrollTop = 0;</script>” to a data label with Literal Checked and Prevent XSS unchecked when clicked.

 

 

Badge +2

Hi @HungTran , I tried your code and it works fine for me.

I did however do it by adding your code (without the <body> tags, because it is already inside a body) to an Expression and setting the Expression Property of the data label.

Please ensure that Literal is checked and that Prevent XSS is unchecked:

 

 

In this example I also achieved the same with a normal button control and by just doing a transfer data with this value: “<script>document.documentElement.scrollTop = 0;</script>” to a data label with Literal Checked and Prevent XSS unchecked when clicked.

 

 

Thank you for advising!!!!!

Reply