Ok, I tried my best in the search, but. I have a task when I need to load some js using Ajax and so on. In short, I'm stuck.
First enter the code in script.js (which I have to load AND, which I cannot change):
var divs = [ '<div class="item">Lorem ipsum 0</div>', '<div class="item">Lorem ipsum 1</div>', '<div class="item">Lorem ipsum 2</div>', '<div class="item">Lorem ipsum 3</div>', '<div class="item">Lorem ipsum 4</div>', '<div class="item">Lorem ipsum 5</div>', '<div class="item">Lorem ipsum 6</div>', '<div class="item">Lorem ipsum 7</div>' ]; delete(divs[3]);
Then my script to load it
$.getScript('script.js', function() { $('.a').append('<div class="yep">' + divs.join('') + '</div>'); $('.item').each(function() { $(this).click(function() { console.log( $('.item').index(this) ); }); }); });
The problem is that when I click, I need to get the index of the element in the array, that is, if I click on βLorem ipsum 4β, the console should print β4β and not β3β, as it is happening now (due to remote element that does not appear in dom). Is there a way to get the correct result using jQuery?
Well, I need to say that this is a task. And here's the thing: I just CANNOT change script.js. Let's say this on the server, and I don't have access to it until I get it. But I need the index of the element that it has in the source array.
kibin
source share