views:

111

answers:

1

I found code online to clear a form but it had nothing for a select box. With jquery i tried adding code to select the default option in the select box when i reset the form. I figured the only way to find the default was to look for where the SELECTED option was. I then discovered when the user selects something else jquery does not see selected as the default but as to the new option the user selected.

How do i get find the default option in the form so i can clear this properly?

//http://www.learningjquery.com/2007/08/clearing-form-data
$.fn.clearForm = function () {
    return this.each(function () {
        var type = this.type, tag = this.tagName.toLowerCase();
        if (tag == 'form')
            return $(':input', this).clearForm();
        if (type == 'text' || type == 'password' || tag == 'textarea')
            this.value = '';
        else if (type == 'checkbox' || type == 'radio')
            this.checked = false;
        else if (tag == 'select') {
            //alert($('option', this).size());
            alert($('option[selected]', this).val());
        }
    });
};
+2  A: 

Usually the default is the first <option> in the <select>, so you can do that:

$(this).find("option:first").attr("selected", true);

Though, if you're resetting an entire form, you can reset it to what it was on page load with a .reset() call, like this:

if (tag == 'form')
  this.reset();

I'm not sure that's what you're really after, but just throwing it out there. Often the native DOM methods already there are very handy, don't ignore them!

Nick Craver
I'm talking about when in html you have a list of 5 things and the html author writes SELECTED in one of the options in the HTML source. which could be last. 2nd last, the 2nd item, etc. If this code was generated i'd inline some JS vars for my external JS file to use but unfortunately the html is not generated.
acidzombie24
@acidzombie24 - In that case the `.reset()` seems like what you want...it resets the whole `<form>` to what it was on page load, including the originally selected `<option>`...your alternative is to store it on page load and restore it later.
Nick Craver
I am unsure why i couldnt use that function but it looks like its doing what i want. (i'll let you know here i guess if i even run into the problem). How do i clear it via jquery? i tested with this `$("form")[0].reset();` however replacing `$("form")[0]` with `$(this)` and `$(this).get()` gets an error. Using Chrome "Uncaught TypeError: Object [object HTMLFormElement] has no method 'reset'"
acidzombie24
@acidzombie24 - It's a DOM method, so just `this.reset()` if `this` is a form element...hopefully this example clears it up: `$("form").each(fuction() { this.reset(); });`
Nick Craver
oh yeah, that makes sense. I dont know what i was confused about. Good answer especially for asking/saying i dont need my own jquery reset (or why it didnt mention it. I see it does but only in comments which i almost never read)
acidzombie24