1

this is a function to call SELECT element values. but i am facing an error. code is here.

function get_s_val(){
var foo = all_categories_1;
var ov1 = "";

    for(m=0;m<=foo.length;m++){
        ov1 += foo[m].value+',';
    }

console.log(ov1);
var tme=setTimeout("get_s_val()", 1000);
}
get_s_val();

it shows an error like "Uncaught TypeError: Cannot read property 'value' of undefined"

but when i do some littel changes it works.. like

function get_s_val(){
var foo = all_categories_1;
var ov1 = "";

    //for(m=0;m<=foo.length;m++){
        ov1 += foo[0].value+',';
    //}

console.log(ov1);
var tme=setTimeout("get_s_val()", 1000);
}
get_s_val();

i dont know that where i am wrong to write the code.

3
  • Why don't you use var for the loop variable, why do you pass a string to setTimeout? Commented Oct 28, 2011 at 11:24
  • what is all_categories_1 actually? Commented Oct 28, 2011 at 11:24
  • @soundar: all_categories_1 is a HTML SELECT element. Commented Oct 28, 2011 at 11:27

3 Answers 3

2

Modify your loop condition to run while the iterator is less than the length of the array, or you'll get undefined when you hit the non-existent element at index foo.length:

for(var m=0;m<foo.length;m++){
    ov1 += foo[m].value+',';
}

...and always declare variables with the var keyword, or bad things will happen, and JSLint will whine about it (and rightly so, but that's another topic).

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

3 Comments

Thanks Karim. it works. but tell me 1 thing, is "=<" or ">=" wrong thing to use.
If you want to iterate over all elements of an array, then yes. The first element of an array is at index 0, so the last item is at total number of elements - 1. Hope that's clear.
yeh Karim, i know that rule.. but i just now understand that where was the problem. thanks alot.
1
function get_s_val(){
    var foo = all_categories_1;
    var ov1 = "";

        for(var m = 0; m < foo.length; m++){ // use var, and only loop from e.g.
                                             // 0 to 2 when the length is 3, so <,
                                             // not <=
            ov1 += foo[m].value+',';
        }

    console.log(ov1);

    setTimeout(get_s_val, 1000); // don't use a string, just pass the function.
                                 // Plus, the variable is nowhere accessible so
                                 // you can drop storing it
}

get_s_val();

Anyway, if you simply want to join the array's elements into a string with , as delimiter, why not do:

console.log(foo.join());

1 Comment

Thanks pimvdb for your answer and for join() function.
0

At the top of the for loop, m<=foo.length; should instead be m<foo.length;.

1 Comment

Thanks Hammerite for answering me.

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.