Reputation: 4857
I'd like to add a JavaScript code inside the onclick
attribute of each link of a list. The code is used for google analytics event tracking.
This is the list:
<ol>
<li>
<a href="">Alberto Guardiani</a>
</li>
<li>
<a href="">Antica Bottega</a>
</li>
<li>
<a href="">Ash</a>
</li>
<li>
<a href="">Ballin</a>
</li>
</ol>
And this is what it should be like after the jquery appends the onclick code:
<ol>
<li>
<a href="" onclick="_gaq.push(['_setCustomVar', 2, 'Designer', 'text1', 3]);">text1</a>
</li>
<li>
<a href="" onclick="_gaq.push(['_setCustomVar', 2, 'Designer', 'text2', 3]);">text2</a>
</li>
<li>
<a href="" onclick="_gaq.push(['_setCustomVar', 2, 'Designer', 'text3', 3]);">text3</a>
</li>
<li>
<a href="" onclick="_gaq.push(['_setCustomVar', 2, 'Designer', 'text4', 3]);">text4</a>
</li>
</ol>
I have no idea if that's even possible with jQuery append
or what else should I do with this issue. Could you suggest some kind of solution?
Upvotes: 0
Views: 1909
Reputation: 9212
$("ol li a").click(function() {
_gaq.push(['_setCustomVar', 2, 'Designer', $(this).html(), 3]);
});
Edit:
If it doesn't work you may have forgot to check if the document is completely loaded.
In this case you should wrap the code.
jQuery(document).ready(function($) {
/* jQuery code */
});
Upvotes: 5
Reputation: 48837
Using jQuery:
$('ol li a').click(function() {
_gaq.push(); // your code here
});
Upvotes: 2
Reputation: 60556
As far as i understand your question, you can do this using the click
handler.
Html
<li>
<a class="someClassName" href="">Alberto Guardiani</a>
</li>
jQuery
$(".someClassName").click(function() {
_gaq.push(['_setCustomVar', 2, 'Designer', 'text1', 3]);
});
Upvotes: 0