I have a list of links that will switch a display section of a div (like a carousel) and each link has an attribute for the slide.
In JavaScript the variable _pagerList
is the list of objects, and each one will move the div to its corresponding slide. The problem is that all of them do the same, so I changed to an alert to see the 'data-slide' value and all of the return 6.
CODE
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Document</title>
</head>
<body>
<ul>
<li><a class="pager-list" href="" data-slide="1"><img src="http://placehold.it/30" alt="" /></a></li>
<li><a class="pager-list" href="" data-slide="2"><img src="http://placehold.it/30" alt="" /></a></li>
<li><a class="pager-list" href="" data-slide="3"><img src="http://placehold.it/30" alt="" /></a></li>
<li><a class="pager-list" href="" data-slide="4"><img src="http://placehold.it/30" alt="" /></a></li>
<li><a class="pager-list" href="" data-slide="5"><img src="http://placehold.it/30" alt="" /></a></li>
<li><a class="pager-list" href="" data-slide="6"><img src="http://placehold.it/30" alt="" /></a></li>
</ul>
<script>
window.onload = function() {
var _pagerList = document.querySelectorAll('.pager-list');
for ( var i = 0; i < _pagerList.length; i++ ) {
var p = _pagerList[i];
p.onclick = function() {
alert(p.getAttribute('data-slide'));
return false;
};
}
}
</script>
</body>
</html>