I'm having trouble with attaching an onclick to each link object inside a loop, when you click the link, it seems to always return data relevant to the first item in the loop, regardless what was clicked. Where as i need each link clicked to have the relevant href to that link
In the example below, regardless what link was clicked, the console.log would always show "http://example.com/link1/"
HTML Example
<li>
<h2><a class="t" href="http://example.com/link1/"><i>Link 1 text</i></a></h2>
<div class="panel" >Content</div>
</li>
<li>
<h2><a class="t" href="http://example.com/link2/"><i>Link 2 text</i></a></h2>
<div class="panel" >Content</div>
</li>
<li>
<h2><a class="t" href="http://example.com/link3/"><i>Link 3 text</i></a></h2>
<div class="panel" >Content</div>
</li>
</ul>
JavaScript:
(function(){
var theh2s = document.getElementById("panelList").getElementsByTagName("h2"),
i = theh2s.length;
while (i--) {
var getTheLinks = theh2s[i].getElementsByTagName("a");
if (getTheLinks){
if (getTheLinks[0].href){
console.log(getTheLinks[0].href);
getTheLinks[0].onclick = function() {
console.log(getTheLinks[0].href);
_gaq.push(['_trackEvent', 'Homepage', 'AB Click - Test B', getTheLinks[0].href]);
};
}
}
}
})();