简体   繁体   中英

Add onclick event to a div

I have a dynamically generated button as below:

<div style="float: left; width: 150px; cursor: pointer;" id="thebutton"  class="orange_form">
            <span style="padding-top: 17px; font-size: 1.5em;">The button</span>
        </div>

To display the button I have this code inside of javascript:

$('div#thebutton').click(function() {
        $('div#Form').show();
        $.scrollTo($('div#Form'), 800);
    });

I want to add GA event tracking to this button:

onClick="_gaq.push(['_trackEvent', 'category', 'action', 'label']);"

Is this correct?

$('div#thebutton').click(function() {
        $('div#Form').show();
        $.scrollTo($('div#Form'), 800);
        _gaq.push(['_trackEvent', 'category', 'action', 'label']);
    });

Or should I create a different script to call this function?

$('div#thebutton').click(function() {
        _gaq.push(['_trackEvent', 'category', 'action', 'label']);
    });

Thanks

the "onClick" element in the button tag will cause the function specified on the other side of the equal sign to be called: "onClick"="foo()" will call function "Foo" when the button is clicked. If you want to run "_gaq.push(['_trackEvent', 'category', 'action', 'label']);" then put that in the function that you specify for the "onClick" element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM