0

I have this function:

function activateControlFlow(arrayControl) {
    $.each(arrayControl, function(){
        console.log(this);
        $(this).css({color:'#FFF', cursor: 'pointer'});  
    }); 
}

and I don't understand why if I pass to this function only one item, prints me that item as the array. Here is my example:

activateControlFlow(["a#nexth"]);

and in the console it prints this:

String {0: "a", 1: "#", 2: "n", 3: "e", 4: "x", 5: "t", 6: "h"}

WHY???

3
  • Can you give a example of arrayControl value being passes? are you sure its an array of dom objects? Commented Nov 21, 2013 at 21:53
  • yes It was an array of Objects, But I didn't know how to do it Commented Nov 21, 2013 at 21:56
  • 1
    A perfectly legitimate question. Just phrased in an odd way. Commented Nov 21, 2013 at 22:01

2 Answers 2

2

The problem is that you're using this instead of a parameter of a function, as defined in the jQuery API (http://api.jquery.com/jQuery.each/)

function activateControlFlow(arrayControl) {
    $.each(arrayControl, function(index, value){
        console.log(value);
        $( value ).css({color:'#FFF', cursor: 'pointer'});  
    }); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would also suggest changing the variable's name from "value" to "selector"
0

So I was using 'a#nexth' instead of $('a#nexth'), which obviously I was sending a string instead of the Object itself.

So it should be like this:

activateControlFlow([$('#nexth')]);

1 Comment

It might be easier not using the jQuery objects. Consider: activateControlFlow(['#nexta', '#nextb', '#nextc']) instead of activateControlFlow([$('#nexta'), $('#nextb'), $('#nextc')])

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.