0

I want to trigger a JavaScript function with a button that needs to do some things before it triggers the script (login).

At the moment the login happens before the href is triggered so thats great. I was wondering if i could trigger a javascript function with the href. So the href would have the functions ID in it?

How would I make that work?

zaxzax
  • 73
  • 1
  • 9

2 Answers2

0

You just need to add onclick="functionname()" in the a tag.

  • in the href tag? if i just add it in the anchor, it doesent work cause it runs the function before the login – zaxzax Feb 27 '18 at 17:09
  • Oh sorry. My mistake. I typed wrong. It has been edited. –  Feb 27 '18 at 17:40
0

First off, remove the href attribute as it is not an attribute supported by the button tag. Instead, add a click event listener to the button using JavaScript.

When the button is clicked the event handler should call a function that will do whatever pre-login stuff that needs to be done. You can then use a callback function to trigger the login. Using a callback guarantees all pre-login stuff will be done prior to login. Something like below:

document.getElementById('myBtn').addEventListener('click', function() {
  doPreLoginStuff(doLogin);
});

function doPreLoginStuff(callback) {
  console.log('Doing some stuff..');
  console.log('Doing some more stuff..');
  console.log('Doing even more stuff..');
  callback();
}

function doLogin() {
  console.log('All pre-login stuff done. Now we can trigger the login script');
  //now do login
}
<button id="myBtn" data-ga="Era_Eurosport anon-client Player-Hero-Tellin CTA" class="btn btn--order usage-needs-auth-strong">
    <span class="btn__text ">Oled Telia klient?</span>
    <svg class="icon btn__icon-right ">
        <use xlink:href="/tk-telia-theme/images/icons.svg#arrow-right"></use>
    </svg>
</button>`
Tom O.
  • 5,730
  • 2
  • 21
  • 35