0

I am trying to check to see if one of these radio boxes is checked and, if it is, to uncheck it and check the next in line.

I'd like to repeat this process every 4 seconds.

<section class="cr-container">
<input id="select-img-1" name="radio-set-1" type="radio" class="cr-selector-img-1 radio-set" checked/>
<input id="select-img-2" name="radio-set-1" type="radio" class="cr-selector-img-2 radio-set" />
<input id="select-img-3" name="radio-set-1" type="radio" class="cr-selector-img-3 radio-set" />
<input id="select-img-4" name="radio-set-1" type="radio" class="cr-selector-img-4 radio-set" />
</section>

i tried something like this but it's not working

$(".cr-container input").each(function(){
    setTimeout( function () {
        requestFunction(data, function(status){
            if ( status == 'OK' ) { 
                if ($(this).attr('checked')) {
                    $(this).next().prop('checked', true);
                }
            }
        });
    }, indexInArray * 400);
});
6
  • 1
    possible scope problem. you have a function within a function within a function. what do you expect this to refer to? can you set up a jsfiddle for testing please Commented Oct 24, 2013 at 16:37
  • 2
    (indexInArray * 400) IS NOT 4 seconds. Commented Oct 24, 2013 at 16:41
  • What is request function? some ajax call? Commented Oct 24, 2013 at 16:43
  • Forget about the jquery i just posted , it seems to be so wrong , can u please give me a code which i can do what i want with it Commented Oct 24, 2013 at 16:45
  • is this what you want? jsfiddle.net/2et3m Commented Oct 24, 2013 at 16:45

2 Answers 2

1

As pointed by @b_dubb, the problem is the in scope since $(this) after two callbacks is no longer the element you wanted.

Try something like that:

$(".cr-container input").each(function(){
    self = this
    setTimeout( function () {
        requestFunction(data, function(status){
            if ( status == 'OK' ) { 
                if ($(self).attr('checked')) {
                    $(self).prop('checked', false); //You wanted to uncheck current element, didn't you?
                    $(self).next().prop('checked', true);
                }
            }
        });
    }, indexInArray * 400);
});

Regarding the 4 sec interval, indexInArray * 400 does not do what you want. Do you want to cheack all elements every 4 secs of do you want to check one element each 4 secs?

BTW a console.log($(this)) might have helped you

EDIT

Since elementcs are radio buttons and not checkboxes there's no need to uncheck the current element, so you can easily omit the line:

$(self).prop('checked', false); //You wanted to uncheck current element, didn't you?

Which would be needed only if the elements were check boxes (multiple select allowed)

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

Comments

0

Let me know if this is what you were looking for:

Here is the jsfiddle: http://jsfiddle.net/BTuaS/

setInterval(repeatProcess, 4000);

function repeatProcess() {
    $(".cr-container input").first().prop('checked', true);
    $(".cr-container input").each(function(i){
        var self = $(this);
        setTimeout( function () {
            requestFunction("", function(status){
                if ( status == 'OK' ) { 
                    if (self.prop('checked')) {
                        self.next().prop('checked', true);
                    }
                }
            });
        }, (i + 1) * 1000);
    });
}

function requestFunction(data, status){
    status('OK');
}

1 Comment

Happy to help. If this answer or any other one solved your issue, please mark it as accepted. Thanks :)

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.