0

I am creating a Javascript cycle, that can be put in the program instead of writing

document.entry.(a_ to z_).value=(ax to zx);

My work so far :

    function param() {
var counter2 = "ax bx cx dx fx gx hx ix jx kx lx mx nx ox px qx rx sx tx ux vx wx xx yx zx";
var alph2=counter2.split(" ");
for (var i=0;i<26;i++){
counter2[i]=1;
}
var letters2 = ".a_ .b_ .c_ .d_ .e_ .f_ .g_ .h_ .i_ .j_ .k_ .l_ .m_ .n_ .o_ .p_ .q_ .r_ .s_ .t_ .u_ .v_ .w_ .x_ .y_ .z_ ";
var let2=letters2.split(" ");

var entry = document.getElementById("entry");
document.entry[let2[i]].value=alph2[i];
}

But Chrome apparently doesn't like the second to last line, saying it cannot set value of undefined - and "entry" is a form in the HTML.

I am a Javascript beginner, so thank you very much for your patience :-)

Jenz
  • 8,280
  • 7
  • 44
  • 77
Jesse_Pinkman
  • 565
  • 1
  • 8
  • 21
  • Maybe you want `document.forms.entry` then? – Bergi Apr 22 '14 at 11:50
  • Do you have any idea what you're doing? Your variable `entry` tells us you don't, as `document.entry` has nothing to do with the variable, and seems like a strange way to access an element ? – adeneo Apr 22 '14 at 11:51
  • adeneo : Quite frankly no, I don't really know what to do now, since I am a beginner. And because of that I was hoping for an explanation, not for a post telling me I am an dumbass ;-) Bergi : nope, that doesn't work – Jesse_Pinkman Apr 22 '14 at 11:54
  • We can't all get what we hope for, sometimes reality bites us the in ass instead. – adeneo Apr 22 '14 at 11:55
  • possible duplicate of [JavaScript - cannot set property of undefined](http://stackoverflow.com/questions/7479520/javascript-cannot-set-property-of-undefined) – Filburt Apr 22 '14 at 11:58

1 Answers1

0
counter2[i]=1;

What is that supposed to do? counter is a primitive string, so this will have no effect at all.

entry.(a_ to z_).value

let2 = [".a_", ".b_", …]
entry[let2[i]].value

If you're using bracket notation, you need to omit the dot from dot notation property access. It should only be the property names in the array:

let2 = ["a_", "b_", …]

Also, you are not looping over the let2 array, but just accessing the ith value in it - where i has the value 26 still. Wrap the for loop around that.


Apart from that, you can generate successive letters easily with String.fromCharCode, you do not need to list the entire alphabet in a string or an array. Better:

var els = document.forms.entry.elements;
for (var i=0; i<26; i++) {
    var chr = String.fromCharCode(97 + i),
        alph = chr + "x",
        lett = chr + "_";
    if (els[lett] != null)
        els[lett].value = alph;
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • thanks, but would you mind guiding me a little with the loop ? I am really just a beginner - thanks – Jesse_Pinkman Apr 22 '14 at 12:00
  • Ehm, and what about the for loop, doing `counter2[i] = 1;` when `counter` is clearly a string? Then there's the `let2` array using `i`, but there is no `i` in that context, it's outside the loop, and the `entry` variable is just like every other thing in the question, it makes no sense. If you can figure out how that code was intended to work, you're really good! – adeneo Apr 22 '14 at 12:00