tags:

views:

62

answers:

4

i have

var 1starray = [1,2,3,4,5,6];
var 2ndarray = [A,B,C,D];

Using Jquery I need to show these two array values on click or on change funtion? with in alertbox?

can anybody helpme out?

thanks

+1  A: 

Like this....

$('selector').click(function(){
  alert(1starray[index]);
});

You need to replace index with whatever index eg 1, 2, 3, A, B

Sarfraz
And of course replace `selector` with the jQuery selector for the element which should be clicked on.
Michael Mior
@Michael Mior: Only OP knows what element, it will happen as he has not provided sample html code for that :)
Sarfraz
Of course, just adding that in case the OP isn't too familiar with jQuery :)
Michael Mior
+2  A: 
var a = ['a','b','c'];

$('selector').click(function(){

  $.each(a, function(i, obj) {
    alert(obj);
  });

});

repeat for second unless you want to merge the two arrays and then display them. Then you would do something like this

var a = ['a','b','c'];
var b = ['1','2','3'];

var c = $.merge(a,b);    
$('selector').click(function(){    
  $.each(c, function(i, obj) {
    alert(obj);
  });
});

Mervyn
Just FYI, in a `each()` loop in jQuery `this` is going to be equal to whatever `obj` is in your example.
gnarf
+1  A: 

if i understand you currectly, you need to show [1,2,3,4,5,6,A,B,C,D]! in this case let's create new array, with all elements of first and second array.

    var array1 = [1,2,3,4,5,6];
    var array2 = ['A','B','C','D'];
    var size1 = array1.length;
    var size2 = array2.length;
    var size3 = size1 + size2;
    var array3 = new Array(size3);
    for(var i=0; i< size1;i++)
    {
        array3[i] = array1[i];
    }
    for(var j=0; j< size2;j++)
    {
        array3[i+j] = array2[j];
    }
    alert(array3);//[1,2,3,4,5,6,A,B,C,D]

but it's javascript, not jquery;)

Syom
Or for simplicity, `var array3 = array1.concat(array2);`
gnarf
This isn't an appropriate solution for merging two arrays.
Mervyn
+2  A: 

Another possible option would be to use array functions which are available in any ECMAScript 3 or above implementation.

alert(a.concat(b).join());
ChaosPandion