Chris
Chris

Reputation: 435

jquery select remove item

My html code

<select name="test[]" id="test_0">
    <option value="">...</option>
    <option value="Test 1">Test 1</option>
    <option value="Test 2">Test 2</option>
    <option value="Test 3">Test 3</option>
</select>
<select name="test[]" id="test_1">
    <option value="">...</option>
    <option value="Test 1">Test 1</option>
    <option value="Test 2">Test 2</option>
    <option value="Test 3">Test 3</option>
</select>

My jquery code

$("#test_0 option:eq(1)").remove();
$("#test_1 option:eq(2)").remove();

If I remove an item, how can I keep it in memory to replace it later at the same place ?

Thanks for your help...

Chris

Upvotes: 1

Views: 849

Answers (4)

Pankaj Sharma
Pankaj Sharma

Reputation: 112

You can use for removing all other option except than one :-

$("#test_0 option.not(':eq(1)').remove();

Upvotes: 0

aziz punjani
aziz punjani

Reputation: 25776

Use .detach instead, it's better suited for reinserting elements at a later time as it preserves data and events attached to the element.

 var option =  $("#test_0 option:eq(1)").detach();
  $('#test_0').append(option); 

Upvotes: 3

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

You could just hide them and show later when needed:

$("#test_0 option:eq(1)").hide();
//and for showing
$("#test_0 option:eq(1)").show();

Upvotes: -1

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You could hide it:

 $("#test_0 option:eq(1)").hide();
 $("#test_1 option:eq(2)").hide();

But im not sure it works on all browsers. Otherwise you could clone it and save it in a variable and then remove it:

var option1 = $("#test_0 option:eq(1)").
var option1clone = option1.clone();
//now remove the original
option1.remove

Upvotes: 0

Related Questions