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
2010-10-22 18:46:07
+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
2010-10-22 18:47:38
Perfect, what if I only want to clear two specific textfields (id1, and id2) how should it be?
fgualda87
2010-10-22 19:04:30
@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
2010-10-23 15:47:21
Yeah, I figured it out by myself, but thanks though! It works perfectly fine.
fgualda87
2010-10-25 13:33:03
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
2010-10-22 19:33:51