0

I have a problem, I have made a script that runs through all images and takes the href of the image and puts it to each buy button under the image. Every image has a buy button underneedth.

But when there is an image without a link the script stops and does not continue to the end.

How could I make it continue till the end?

Found somewhere that return non-false; is like continue, but did not work.

Not allowed to put markup sorry.

$('.productImg a').each(function(){
    if($(this).attr('href').indexOf("sometext")> 0 && $(this).attr('href').indexOf("sometext") != 'undefined'){
       $(this).parents(".item").find(".BuyLink a").attr('href',$(this).attr('href'));
    }
    else if ($(this).attr('href').indexOf("sometext") == 'undefined') {
        return non-false;
    }
});
5
  • return true for continue and return false for break; Commented May 8, 2014 at 10:57
  • 1
    There is no value "non-false". If you return false it breaks, if you return anything else it does not. So I suspect the page you found was saying "return anything which is not false". You could return "foo" if you want. But return true is the best advice. Your code is creating an error because your are returning an undefined variable I suspect. Commented May 8, 2014 at 10:59
  • 3
    indexOf(...) will never, ever return the string undefined. If the string is not found it returns -1. Commented May 8, 2014 at 11:00
  • even, in comparison, if undefined is not string then comparison is wrong, either !== 'undefined' or != undefiend Commented May 8, 2014 at 11:01
  • oops you are right @lonesomeday Commented May 8, 2014 at 11:01

1 Answer 1

1
$('.productImg a').each(function(){
    if (!$(this).attr('href')) // <--- This checks if the image has a link
        return;

    if($(this).attr('href').indexOf("sometext")> 0 && $(this).attr('href').indexOf("sometext") != 'undefined'){
       $(this).parents(".item").find(".BuyLink a").attr('href',$(this).attr('href'));
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

That was clean bro, thanks. It checks in the beginning if there is not a link and if it doesnt continues to the next one?
Sure, if the current element doesn't have href attribute it will continue the loop.

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.