Reputation: 169
Hi I have 3 javascript arrays that need to be passed in a function when one of the 3 available button is clicked (one button for each array)
<button onclick="call(1)">Array1</button>
<button onclick="call(2)">Array2</button>
<button onclick="call(3)">Array3</button>
JS-
var arr1 = ["s","o","m","e"];
var arr2 =[];
var arr3 =[];
function call(x){
// some code here
if(x==1){
another_func(arr1);
}
else if(x==2){
another_func(arr2);
}
else if(x==3){
another_func(arr3);
}
}
This is working fine but I wish to get rid of that if else loops. Since array names differ by only the numeral 'x' (x is passed by the button when clicked) I tried-
function call(x){
// some code here
var apple="arr"+x;
another_func(apple);
}
But this turned haywire. I know the reason that since javascript is loosly typed language it interpreted latter parameter as a string instead of an array.
Could anybody suggest how should I proceed?
Upvotes: 0
Views: 93
Reputation: 8898
You can get a working version in this jsfiddle (I have used jQuery). Your code should be the following by just changing the call()
function to use another_func()
instead of alert()
<button class="a" input="arr1" >Array1</button>
<button class="a" input="arr2" >Array2</button>
<button class="a" input="arr3" >Array3</button>
$( document ).ready(function() {
var arr1 = ["s","o","m","e"];
var arr2 =[];
var arr3 =[];
function call(x){
another_func(x);
}
$('.a').click(function(){
call(eval($(this).attr("input")));
})
});
However, from your own version to this dynamic one, I had to make the following changes:
call()
) are available in HTML elements if needed.call()
function by adding an input
attribute in button elements, defining which variable will be passedeval()
function, so that string passed by input attributes are evaluated to the corresponding variablesUpvotes: 0
Reputation: 752
You could try using a multi dimensional array.
Change your code like this
var arr = [];
arr[1] = ["s","o","m","e"];
arr[2] =[];
arr[3] =[];
<button onclick="another_func(arr[1])">Array1</button>
<button onclick="another_func(arr[2])">Array2</button>
<button onclick="another_func(arr[3])">Array3</button>
Here is a working jsfiddle - http://jsfiddle.net/ao7jL1Lm/1/
Upvotes: 1
Reputation:
Possibly you can go for temp array:
var arr1 = ['f', 'i', 'r', 's', 't'];
var arr2 = ["s", "e", "c", "o", "n", "d"];
var arr3 = ["t", "h", "i", "r", "d"];
var temp = [arr1, arr2, arr3];//work around
var call = function(x) {
alert('Array >> ' + temp[x - 1]); // -1 to get the index
};
<button onclick="call(1)">Array1</button>
<button onclick="call(2)">Array2</button>
<button onclick="call(3)">Array3</button>
OR, you can go for temp object:
var arr1 = ['f', 'i', 'r', 's', 't'];
var arr2 = ["s", "e", "c", "o", "n", "d"];
var arr3 = ["t", "h", "i", "r", "d"];
var temp = {
'1': arr1,
'2': arr2,
'3': arr3
}; //work around
var call = function(x) {
alert('Array >> ' + temp[x]);
};
<button onclick="call(1)">Array1</button>
<button onclick="call(2)">Array2</button>
<button onclick="call(3)">Array3</button>
Upvotes: 1