0

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 function? Within alertbox?

Can anybody help me out?

4
  • Do you want to show the raw arrays? Commented Jun 22, 2010 at 16:51
  • 2
    I think your cuestion is not clear enough. Do you wanna show it like a string or one by one? Can you explain better the desired result? Commented Jun 22, 2010 at 16:51
  • Yes Greg i want two show the two arrays same time in click Commented Jun 22, 2010 at 17:06
  • do you want to see [1,2,3,4,5,6,A,B,C,D] ??? Commented Jun 22, 2010 at 17:17

4 Answers 4

2
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);
  });
});

Sign up to request clarification or add additional context in comments.

1 Comment

Just FYI, in a each() loop in jQuery this is going to be equal to whatever obj is in your example.
2

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

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

Comments

1

Like this....

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

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

3 Comments

And of course replace selector with the jQuery selector for the element which should be clicked on.
@Michael Mior: Only OP knows what element, it will happen as he has not provided sample html code for that :)
Of course, just adding that in case the OP isn't too familiar with jQuery :)
1

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;)

2 Comments

Or for simplicity, var array3 = array1.concat(array2);
This isn't an appropriate solution for merging two arrays.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.