-1

Why the for loop does not run?

  • The Error-Console is empty
  • The function countfiles return a whole number
  • parseInt(countfiles()) also not work too

Here my Code:

function set_default_value() {
    var element = document.getElementsByName('fields');
    for (var i = countfields(); i < element.length; ++i) {
        element[i].value = "default";
    }
}

Why does not run?

EDIT:

  • Countfields is lower than element.length
  • The function is running
  • Countfield return a number like 0, 1, 2, 3, ...

Here's the function countfields

function countfields() {
    var field_counter = 0;
    for (var i = 0; i < 50; i++) {
        if (typeof document.getElementsByName('other_fields')[i] != "undefined") {
        var field_counter = field_counter + 1; 
        }
    }
    return field_counter; 
}
8
  • 2
    I would provide the source to countfields() and also the related markup Commented Feb 11, 2013 at 17:51
  • 5
    The number returned by countfields could be greater than element.length Commented Feb 11, 2013 at 17:52
  • what does countfields() return and also paste some snippet of your html structure Commented Feb 11, 2013 at 17:52
  • yes, perhaps countfields() is greater than element.length Commented Feb 11, 2013 at 17:52
  • Is countfields() >= element.length? Commented Feb 11, 2013 at 17:53

3 Answers 3

1

@varunachar touched on the issue in one of his comments:

The number returned by countfields could be greater than element.length

In this line:

for (var i = countfields(); i < element.length; ++i)

Read this i < element.length as "as long as i is less than element.length, do stuff". Or, if countfields() is equal or greater, nothing happens.

1 other_fields: http://jsfiddle.net/D5ZHV/ works
6 other_fields: http://jsfiddle.net/Ay6BN/ does not work

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

Comments

0

Change

        var field_counter = field_counter + 1; 

To

        field_counter = field_counter + 1; 

In countfields() function. You are declaring field_counter inside the for loop which is wrong

1 Comment

That indeed looks incorrect, but in JavaScript it will still work the same way.
0
function countfields() {
    var field_counter = 0;
    for(var i = 0; i < 50; i++) {
        if(typeof document.getElementsByName('other_fields')[i] != "undefined") {
        var field_counter = field_counter + 1; } }
    return field_counter; }

var always define the scope of the variable. Inside the if doing var field_counter will create a variable with scope inside the if-statement.

you should write as

field_counter = field_counter + 1; 

If you test your countfields() it should always return 0;

Also try doing console.log(elements)

Comments

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.