0

I am writing a bookmarklet to gather information from my Indiegala bundles I bought. Instead of gifting the whole bundle I am gifting onesies and twosies at a time so having the individual gift urls makes it easier.

Before they had it so the key was there and I could just gather it with a simple selector, now they have a little image you have to click on to get the gift url for each steam/desura game.

I would like to, in a loop, click all of these gift images to get the gift url to appear and then loop through and get those gift urls. I wrote this:

var gifts = $('#icon-gift img');
for(var j=0; j < gifts.length-1; j++){
  gifts[j].click();
}

to loop through all the images and trigger the click so the gift url will appear. I know it goes through each of the images because it logs to the console so I know it isn't getting stuck but for some reason it ONLY clicks the first image available. After you click an image (thankfully) it is clicked forever and when I refresh it only has the gift url then if I run the loop again it does the very next one (but that defeats the purpose of doing it in a script).

I have tried adding a delay(500) and even delay(1500) appended to the click() but that doesn't seem to change anything.

My question is: does anyone know why this would happen and is there any advice on how to fix or get around this?

More thoughts and info

I think it has something to do with asynchronicity. It's like the rest of the loop does everything else except for the other click event. I tried: var gifts = $('#icon-gift img');

$.each(gifts, function(i, val){
  setTimeout(function(){
    val.click();
    val.remove();
  }, 1000);
});

and it still only does the first one and removes all the other images. If i manually go through gifts[0].remove(); then gifts[0].click(); it does the very next one. This is exactly what I thought the .each() or a for loop would do, but it seems like it can't execute the next click with the previous one still being executed or still present.

3
  • Are you purposefully not clicking the last image? Also, what is the output of gifts.length? Commented Aug 2, 2014 at 5:51
  • Why are you using a for loop? jQuery has a built-in each method to do this. Commented Aug 2, 2014 at 5:54
  • @DJDavid98 I am just used to it and didn't think about the each method. @JonathanCrowe if there are 7 items the length is 7 but it starts with 0 so i have to subtract 1 to not get an out of range error. Commented Aug 2, 2014 at 8:17

1 Answer 1

5

No need to use for loop

$('#icon-gift img').click();

is quite enough. It will perform a click on all the passed selectors #icon-gift img -> which are all the descendant images of #icon-gift. All of them.

Otherwise you code was not working cause using gifts[j] you're actually extracting from the Array of your jQuery selectors the JS reference to the DOM HTML node element, therefore not any more a jQuery Object element. Wrapping it again in an jQuery object would work $(gifts[j]) or using the eq() selector like gifts.eq(j), but again not needed as I demonstrated above.

Sign up to request clarification or add additional context in comments.

2 Comments

I just tried this (and pretty sure I tried this before when I first found out about click() and it only does the first one and on subsequent pastes of the code into the console it doesn't go on to do the second one it's like it is stuck on the first node. I tried doing a remove() on the node after the click but it clicks the first one and all the remaining image nodes disappear without getting clicked.
@ThomasLe Are you sure that there is no javascript error in the console? This example works: jsfiddle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.