views:

110

answers:

4

I have a search field, and I need a clear button.

I currently have the button, but I don't know how to do it,

I have 6 textfields, 2 comboboxes, and 2 multiple select lists

How do I clear all of them in one clear function?? I know the HTML way, but I am using Grails and the type="reset" does not work. That's why I want a jQuery way of deleting textboxes' values, leaving the combobox in the first index, and clear all options from the multiple select list.

Thanks for the help :D

+7  A: 

If you have a form just add a input with type reset

<input type="reset" value="Clear the Form" />

If you can't use this, then save the default values using .data and retrieve them on you reset the form.

See this example on jsFiddle

$("#container :text").each(function() {
    var $this = $(this);

    $this.data("default", $this.val());
});

$("#container select option").each(function() {
    var $this = $(this);

    $this.data("default", $this.is(":selected"));
});

$("#container :button").click(function() {
    $("#container :text").each(function() {
        var $this = $(this);
        $this.val($this.data("default"));
    });

  $("#container select option").each(function() {
      var $this = $(this);
      $this.attr("selected", $this.data("default"));
  });
});

HTML

<div id="container">
    <input type="text" value="default" />
    <select>
        <option>Op1</option>
        <option selected="true">Op2</option>
    </select>
    <select multiple="true" size="5">
        <option>Op1</option>
        <option selected="true">Op2</option>
    </select>

    <input type="button" value="reset" />
</div>

To clear all inputs and remove all options on select elements its more simple, see this example on jsFiddle (same html).

$("#container :button").click(function() {
    $("#container :text").val("");

    $("#container select").empty();
});
BrunoLM
I'm actually using Grails, so that won't work.
fgualda87
@fgualda87: What about now? :P
BrunoLM
LOL, much better, but for the multiple select list, I want it to be blank, like remove all the options that it has. Like since is a search field, I add things to the list, and with the clear button it should erase all of them. Now, for demonstration purposes I only have to values, maybe if I erase them and leave it like that from the beginning it'll work?? Thanks
fgualda87
To erase an input field just set val to empty: `val("")`. And to remove all options call `empty`. `$("#container select").empty();`
BrunoLM
+1  A: 

Use Lee Sy En's solution that he found on SO. It's much better and takes care of everything.

$('#myClearButton').click(function() {
  $('#myTextBox').val('');

  $('#myComboBox').val('0'); // set to default value as an example i use 0 
});
gmcalab
What about the combo box? And the multiple select list??
fgualda87
+5  A: 

You can modify the code below to suit your needs. It's stolen from this thread anyway.

jsfiddle

$(':input','#myform')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');

<form id='myform'>
    <input type='text' value='test' />
    <select id='single'>
        <option>One</option>
        <option selected="true">Two</option>
    </select>
    <select multiple="true" size="5" id='multiple'>
        <option>One</option>
        <option selected="true">Two</option>
    </select>
    <input type='button' id='reset' value='reset' />
</form>


EDIT (To clear multiple select):

$('#reset').click(function(){
    $(':input','#myform')
    .not(':button, :submit, :reset, :hidden')
    .val('')
    .removeAttr('checked')
    .removeAttr('selected');

    $("#myform #multiple").empty();
});​

jsfiddle v2

Lee Sy En
This is the way to go no doubt. +1
gmcalab
isnt that missing like a .each? or function (...) ???
fgualda87
Ok, almost perfect. I need to leave the multiple select empty. With no options in it.
fgualda87
Each jQuery function that returns a jQuery collection (like val(), not(), removeAttr()) has the .each() built in, e.g. http://github.com/jquery/jquery/blob/master/src/attributes.js#L18
cpharmston
Ok, I fixed it, thanks to <b>BrunoLM</b>.
fgualda87
Here is the jQuery `$('#reset').click(function(){ $(':input','#myform') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .removeAttr('selected'); $("#myform #list").empty();});`
fgualda87
nice.. I have updated the JQuery to clear multiple selection box as well.
Lee Sy En