views:

40

answers:

3

I have a select field at my HTML page that is populated with a few default items. With the help of AJAX, I replace the content (options) in the select field with appropriate options.

How can I store the contents (options) of my select field into a variable and then load it into the select again? It would be helpful to be able to get back the default options of the select field.

Thanks!

A: 

My way would be using the jQuery's .data function to store the options html.

select_box = $("#select-box");
select_box.data("orginal_options", select_box.html());
select_box.html("<option value='1'>New Option</option>");

To resotre:

select_box.html(select_box.data("original_options"));

Of course these are the basic ideas, as I don't really know how's your logic. (And please correct me if I'm wrong).

PeterWong
@peter Yes i forgot about .data, this is very nice feature in jquery.
gov
i was using jquery.metdata.js plugin to create and access dynimaic data.
gov
In fact we could keep the stored data in sequence so that to have a history of the change of select's content. `select_box.data("original_options").push(select_box.html());` if you have initialized it to an array.
PeterWong
A: 

You could use jQuery's .clone() method on the original elements in order to keep a reference to a copy of in the original state.

var $default_opts = $('#mySelect').children()//.clone();

I used jQuery's .children() here to keep it a little more generic in case you're using <optgroup> elements.

When it comes time to revert to the originals, you could do something like this:

$('#mySelect').empty().append( $default_opts.clone() );

This empties the <select> of its current content, and appends a clone of the defaults so that your copy is retained in its original state.


EDIT: I misread part of the question. You wouldn't need the first .clone().

patrick dw
A: 

If you want to restore the objects instead of the html()-string, you can use detach()

//remove options from select and hold them inside a variable
var defaults=$('select option').detach();

//clear select and restore the defaults
$('select').empty().append(defaults);

An example how to use it with replaceWith() :

<select id="target" size="3">
 <option>a
 <option>b
</select>
<input type="button" 
   value="replace"
   onclick="fx($('#target'),this)"
   />

<script  type='text/javascript'>
function fx(o,b)
{
 if(b.value==='replace')
 {
  o.data('defaults',$('option',o).replaceWith('<option>1</option><option>2</option>'));
  $(b).val('restore');
 }
 else
 {
  $(o).empty().append($(o).data('defaults'));
  $(b).val('replace');
 }
}
</script>

http://jsfiddle.net/doktormolle/Sgn72/

Dr.Molle
The `.detach()` method isn't quite right for this. I don't think OP wants to actually remove the elements from the DOM, but rather wants to keep a reference to them in their original state.
patrick dw
html() and clone()/cloneNode() won't give a reference to the original objects. If he wants to replace the defaults instead of removing them first, he can use replaceWith(), this will return the removed options too.
Dr.Molle
Yes, `.replaceWith()` would be good, but what are you replacing it with? You'd still need to (by some means) make a clone of the originals to place back in. Same if you `.detach()`. :o)
patrick dw
No, I don't need to clone anything, replaceWith() replaces something with another and returns the replaced objects. These replaced objects are removed from the DOM, but not deleted - so you can store them to a variable and inject them later back to the DOM.
Dr.Molle
Yes I know that. But what is the *"another"* that `.replaceWith()` is appending? OP wants to retain the originals in a pristine state, yet still wants to manipulate the same elements on the page. As such, you need originals, and a copy. One on the page to be manipulated, and the other referenced in a variable ready to be inserted to bring the `<select>` back to its original state.
patrick dw
The "another" are the new options, added an example above.
Dr.Molle
Ah yes, you're right. I was reading the question incorrectly. Yes, you could detach, and the load in the alternate options.
patrick dw