tags:

views:

82

answers:

5

Hello

Here are the two divs

<div id="1">

<span id="a1">first</span>
<span id="a2b">first</span>
<span id="a3">first</span>

</div>
Click
<div id=2></div>

And the jQuery code to do

  $('#2').bind('click',function(){
            var xx = $('#a'+i).attr('id');
            $('#2').append(xx);
            i=i+1;
        });

It does not get all the 3 id's from #1

Thanks Jean

A: 

well, you don't match "a2b" by just counting "i" up there.

but your approach is 'bad manner' anyway, try something like:

$('span').each(function(){
  $(this).appendTo($("#2"));
});

Kind Regards

--Andy

jAndy
+1  A: 

First of all, element IDs should start with a letter or the underscore character. Second of all, try this:

$("#second").click(function() {

    // grab the ids of the first div's spans into an array
    var ids = $("#first span").map(function() {
        return this.id;
    }).get();
    alert(ids);

    $(this).append(ids.join(",")); // or whatever
});

See http://api.jquery.com/map/

karim79
@karim79 please let go off the small bits, focus on helping me out with the answer, I altered the code for stackoverflow
Jean
If you look carefully, you'll see that the above example grabs the three IDs from the elements in the first div, and stores them in the variable `ids`.
karim79
@karim let me try them, Is it not possible to do it without an array, I guess not..
Jean
A: 

In your click function, variable 'i' is used before being set and this may explain your problems.

Can you check if initializing the variable helps?

zaf
A: 

You may try this:

 $('#2').bind('click',function(){
       var dest = $(this);
       $("#1").find("span").each(function(){
           dest.append($(this).attr('id'));
       });
  });
Satoru.Logic
A: 

Hard to tell exactly what you want, but couldn't you do this to move all of the elements:

$("#1").children().appendTo("#2")
Eric