0

im practicing javascript .

and im trying using array element as array name in Javascript .

i searchd stackoverflow and found some links like this

when im testing this i found that its not working, it gives undefind when im calling the second function. please see the jsbin link , maybe im getting it wrong please help me out. here is my code , and link: https://jsbin.com/yazowutera/

<button onclick="testArrays1(a, [vic.zero, vic.one])">source</button>
<br>
<button onclick="testArrays2(a, vic[all[1]])">desti</button>

and javascript

<script>

var vic = {

    zero : ["00","01","02"],

    one : ["10","11","12"],

    two : ["20","21","22"]

};
var all = ["zero","one","two"];

var a = "a";


function testArrays1(needle, arrays){
    for (var i=0; i<arrays.length; i++) {
        alert(needle +arrays[i]);
    }
}

function testArrays2(needle, arrays) {
  //for (var i=0; i<arrays.length; i++) {
    alert(needle +arrays);
  //}
}

</script>

im not in j-query right now please ans in javascript

Community
  • 1
  • 1
victor
  • 57
  • 9

3 Answers3

1

It seems that all is a special variable reserved for HTMLAllCollection and the second element of it (i.e. all[1]) is a HTMLHeadElement. This only seems to applicable when working with inline scripts (scripts in element attributes). It seems you have to do document.all if using normal script blocks (but this may be caused as a side-effect of testing with JSFiddle).

The recommended solution is to use a different variable name. For example, change the variable to test:

<button onclick="testArrays2(a, vic[test[1]])">desti</button>

var test = ["zero","one","two"];
musefan
  • 47,875
  • 21
  • 135
  • 185
  • where can i find list of all these reserved things because i wasted 2 hours for finding this cheap solution – victor Jul 13 '15 at 13:48
  • I wouldn't concern yourself with a list. Just learn how to debug your issues better. A simple `console.log(all)` would have shown you that something was wrong with `all`, then a quick search would have got you the answer. Try to avoid using inline scripts, create a `script` block and register your button clicks in that - though you will want to do it on document ready (or equivalent) – musefan Jul 13 '15 at 14:11
  • thnx for the tip, have you own any website or blog or something online , where you post about programing. – victor Jul 13 '15 at 15:19
  • @victor: Sorry I don't. I just try to help on here when I get the chance – musefan Jul 13 '15 at 16:25
1

"all" (or "document.all") is a reserved variable that returns an HTMLAllCollection. Change your variable name to something else in your javascript and it will work.

kanzelm3
  • 535
  • 3
  • 12
0

As metioned by others all is a reserved keyword. But if you still want to use it you can use as below. Which works!

Remember: Always using a reserved keyword as variable in JavaScript coding is a bad practice. So refrain using those reserved words.

Your Javascript:

window.all = ["zero","one","two"];

Your HTML:

<button onclick="testArrays2(a, window.all[1])">desti</button>
NiRUS
  • 3,901
  • 2
  • 24
  • 50