Call 2 events on onclick

1

I'm developing a website, and I need to trigger 2 events when a text is clicked. A js function, and a google analytics goal tracking code. Here's an example

function shownumber() {
    document.getElementById("number").innerHTML = "<small>(62)</small> 9999-9999";
}
<p class="fixed__box__number" id="number">
    <small>(62)</small> 9999-99...
    <span onclick="shownumber();ga('send', 'event', 'Telefone', 'Clicar');">ver telefone</span>
</p>

As I'm doing on localhost I can not test whether the 2 are working. And I think I'm not, because I changed the call order to the function and did not activate the function, so the code I sent here is not working.

How would it be done to add 2 onclick events with native JavaScript? There is another way to make it easier / right.

    
asked by anonymous 12.11.2016 / 19:59

1 answer

3

You can do this in two ways:

Use (A(), B(), C()) to invoke multiple actions:

onclick="(foo('html'), bar('html'))"

Or do everything in JavaScript:

var button = document.querySelector('button');
button.addEventListener('click', function() {
    foo('addEventListener');
    bar('addEventListener');
});

An example with both versions below:

function foo(quem) {
    console.log('foo', quem);
}

function bar(quem) {
    console.log('bar', quem);
}

var button = document.querySelector('button');
button.addEventListener('click', function() {
    foo('addEventListener');
    bar('addEventListener');
});
<button type="button" onclick="(foo('html'), bar('html'))">Clica-me</button>

jsFiddle: link

    
12.11.2016 / 21:57