views:

71

answers:

3

hello

i have a Select input and it's name is "item" for example

<select name="item">
<option value="1">1</option>
<option value="2" selected="selected">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>

i want to add a button that will copy this Select and removed Selected value if any and then append that copy to an a div called "all items".

how can i do that in Jquery? thanks

A: 

Give your select an id.

$('#button').click(function(){
       $('div#id').append($('#id option:selected').val()); //Append.
       $('#id option:selected').remove(); //Remove selected item.
    }
Aseem Gautam
+1  A: 

Demo Online: http://jsbin.com/awuzo/edit

From what I understand, you want to remove what is selected, and then transfer the select element itself to a new location?

$(function(){
  // when we click our button
  $("#clickme").click(function(){
    // Move our SELECT and remove Selection value(s)
    $(":input[name='item']")
      .appendTo("#allitems")
      .children(":selected").remove(); 
  }); 
});
Jonathan Sampson
$(":input[name='item']") ??????
Reigel
@Reigel: What is the problem?
Jonathan Sampson
I was asking why input?... did I missed something?
Reigel
@Reigel. Yes, you missed the colon :) `:input` from the jQuery site: "Selects all input, textarea, select and button elements"
Doug Neiner
ahh Okay... did not know that... cool ;))
Reigel
Doug Neiner, the walking jQuery Documentation, to the rescue :) Hehe.
Jonathan Sampson
@Reigel: jQuery is a glorious framework - you'll find yourself learning something all the time. I do at least :) Largely at the correction of Mr. Neiner himself ;)
Jonathan Sampson
Yeah!.. I sure want to learn more!... cheers!
Reigel
A: 

i want to add a button that will copy this Select and removed Selected value if any and then append that copy to an a div called "all items".

$('#button').click(function(){
   var copy = $('select[name=item]').clone();
   copy.children(':selected').removeAttr('selected'); // just unselect....
   // copy.children(':selected').remove() // if you want to remove the element...
   $('#div_all_items').append(copy); //Append.
})
Reigel
that's works for a single click but it will not create another select if i click the button multiple times
From.ME.to.YOU
are you sure it's not working? You can have a demo of that here http://jsbin.com/atiho/2 and it did sure work.
Reigel