0

I have code:

var links = document.querySelectorAll('a[data-lightbox]');

for (var i = 0; i < links.length; i++) {

  links[i].addEventListener('click', function() {
    event.preventDefault();
    var imgLink = this.getAttribute('href');
    var imgTitle = this.getAttribute('title');
    var dataLightbox= this.getAttribute('data-lightbox');
    console.log(); //next element after "this." something like "links[i+1]" or i don't know...
  }, false);
}

I want to get 'data-lightbox' attribute for next element which I clicked currently. How to do it?

Damian
  • 3
  • 3

4 Answers4

0

Using a IIFE can do the trick to preserve the i scope

var links = document.querySelectorAll('a[data-lightbox]');
for (var i = 0; i < links.length; i++) {
    (function(i){
       links[i].addEventListener('click', function() {
        event.preventDefault();
        var imgLink = this.getAttribute('href');
        var imgTitle = this.getAttribute('title');
        var dataLightbox= this.getAttribute('data-lightbox');
        console.log(links[i + 1]);
      }, false);
    })(i)

    }
ocespedes
  • 1,233
  • 4
  • 14
  • 27
  • Yeah, it works! Thanks for quick answer. :) Also i try other solutions from here. Thank you. – Damian Dec 07 '16 at 21:04
0

You can try to get the next element in the way you thought: links[i + 1], although the i is an unique hoisted variable by this loop. You can, however, re-generate this i in the loop body, using variable declaration of let (only supported in ES6+) or using a new function scope inside that loop.

let acts like we were in a new scope, but not. It won't affect the previous i in this example, it'll only replace its presence at the block statement.

var links = document.querySelectorAll('a[data-lightbox]');

for (var i = 0; i < links.length; i++) {

  let i = i;

  links[i].addEventListener('click', function() {
    event.preventDefault();
    var imgLink = this.getAttribute('href');
    var imgTitle = this.getAttribute('title');
    var dataLightbox= this.getAttribute('data-lightbox');
    console.log(links[i + 1]);
  }, false);
}
0

This is a scope issue.

You can use bind (which would fix the scope issue) for the onclick event binding,while this you can send i to the method and you can access the next element using i+1

check the following snippet

window.onload = function() {
  var links = document.querySelectorAll('a[data-lightbox]');


  for (var i = 0; i < links.length; i++) {

    links[i].addEventListener('click', onclick.bind(links[i], i));
  }

  function onclick(i) {

    var imgLink = this.getAttribute('href');
    var imgTitle = this.getAttribute('title');
    var dataLightbox = this.getAttribute('data-lightbox');
    if(links[i+1]!=undefined){
    var nextLightbox = links[i + 1].getAttribute('data-lightbox');
      }

    console.log(imgLink);
    console.log(dataLightbox);
    console.log(nextLightbox);
  }
}
<a href="#" data-lightbox=10>link1</a>
<a href="#" data-lightbox=20>link2</a><a href="#" data-lightbox=30>link3</a><a href="#" data-lightbox=40>link4</a><a href="#" data-lightbox=50>link5</a>

Hope it helps

Geeky
  • 7,420
  • 2
  • 24
  • 50
0

In addition to what others have mentioned, another way to go about this is using nextSibling on this.

var links = document.querySelectorAll('a[data-lightbox]');

for (var i = 0; i < links.length; i++) {

  links[i].addEventListener('click', function() {
    event.preventDefault();
    var imgLink = this.getAttribute('href');
    var imgTitle = this.getAttribute('title');
    var dataLightbox= this.getAttribute('data-lightbox');
    console.log(this.nextElementSibling);
  }, false);
}
arbybruce
  • 188
  • 1
  • 11