Custom macro in a JavaScript Inside HTML transformation

Novice User asked on May 28, 2020 22:45

Hi,

In my HTML transformation , I am using a href link with on-click JavaScript method. Problem is that LikingClick function run every time when the page load and also runs on click event . Whole it should only execute on click event of the Href. How do I make this function only execute once only on click event.

Here is the sample of my transformation.

<p><b>{%Title%}</b><br> <a href='' onclick = 'Clicktest();return false;' id = 'testid'>Link Text </a></a>

<script>

function Clicktest()
{
   var heartcolor = {%LikingClick(DocumentID,CurrentUser.UserName)#%}   ;
  // do some thing
}
</script>

Correct Answer

Zach Perry answered on May 29, 2020 16:47

Not sure why that would be getting called on load, but have you tried to use Jquery OnClick event of the link ID?

something like this:

   <script type="text/javascript">
        $(document).ready(function() {

            //Link click
            $('#testid').on("click",
                function(e) {
                    var heartcolor = {% LikingClick(DocumentID, CurrentUser.UserName) #%};
                    // do some thing
                });
        });
    </script>

**EDIT: If Like Click is doing something like updating a database table, it will run each time the transformation loads - and if the transformation is in a repeater, it will run for each item in the repeater. The code above is just assuming the likingclick is returning a value and not updating anything. If you are updating something, you would probably need to do an ajax call.

0 votesVote for this answer Unmark Correct answer

   Please, sign in to be able to submit a new answer.