views:

41

answers:

3

I wanna make a function that erases everything inside of a multiple select using jQuery. Also if it could remove some text from text fields it'd be good too.

A: 

Use .empty() to remove child elements from another element.
Example: $("#selectid1,#selectid2").empty()

Without your HTML, it's hard to go into any more detail.

Drackir
+3  A: 

Try the following to clear all multiple select elements:

$('select[multiple]').empty();

Replace select with a specific element if you do not want to clear all of them. The same applies to the following chunk of code. It can be used to clear all text fields:

$(':text').val('');
elusive
Perfect, what if I only want to clear two specific textfields (id1, and id2) how should it be?
fgualda87
@fgualda87: If you specified ID's on your elements, you can select them like this: `$('select#select-id[multiple]')` or `$('#text-id')`. You should take a look at jQuery's [Selector API](http://api.jquery.com/category/selectors/).
elusive
Yeah, I figured it out by myself, but thanks though! It works perfectly fine.
fgualda87
A: 

In case you just want to deselect everything and not remove from the DOM, try


$("select").val([]);

or something with a more specific selector.

MPD