Skip to content Skip to sidebar Skip to footer

How To Pass Array Values To Javascript Function From Html Onchange Action?

I have a separate array like 1 1 1 2 2 2 3 3 3 I need to pass this array to JavaScript function as a parameter while onChange action is called. can anyone give me a suggestion fo

Solution 1:

Try something like below

var x1=[1,11,11];
var x2=[2,21,22];
var x3=[3,31,32];
var y=[x1,x2,x3];
onChange(y);

Solution 2:

Don't know if that's an option, but this is how it may work in jQuery:

HTML:

<textarea id="target" />

jQuery:

$(document).ready(function(){
    $('#target').change(function() {
      var arr1 = [1,2,3];
      var arr2 = [1,2,3];
      var arr3 = [1,2,3];
      var arr = [arr1, arr2, arr3];
      //do whatever you want with italert(arr);
});
});

I guess you could assign it to some hidden element (like input hidden) and then write some code that would read the arr value from it.

At least that's one of options - I'm not sure what you're trying to achieve.

Hope this helps

Solution 3:

You need to wrap the function call in another anonymous function.

element.addEventListener('change', function(e){
    yourFunction(yourArray);
});

Post a Comment for "How To Pass Array Values To Javascript Function From Html Onchange Action?"