tags:

views:

405

answers:

3

I have a function that clears all the elements inside a span id. clearForm() works but resetForm() does not.

`<script type="text/JavaScript">`  
`    $(function hideMe() { ` 
`        for(i=0;i<arguments.length;i++) {`  
`        $("#"+arguments[i]).clearForm().fadeOut();`  
`    }`  
`    });`  
`</script>`  
`<html>`  
`...<td><input type="radio" name="third_party" id="third_partyNo" value="No" onClick="hideMe('party_integ');" />No</td>`  
'...'  
`<span id="party_integ" style="display:none">`  
`<table>`  
`    <tr>`  
` <td width="25%">System Type:</td>`  
` <td width="22%"><select name="system_type" id="system_type" class="style3" onChange="popCat(this.value);">`  
` <option value="" selected="selected">Select one</option>`  
` <option value="first">first</option>`  
` <option value="second">second</option>`  
` <option value="third">third</option>`  
` </select></td>`  
`    </tr>`  
`</table>`  
`</span>` 
A: 
Pointy
It's perfectly valid to have an input with no form. But yeah, that would stop you being able to reset the whole form at once.
bobince
+1  A: 

$("#"+arguments[i]).clearForm()

clearForm isn't the same thing as resetForm.

resetForm sets form fields back to the value​s they had when the page was loaded, ie. the same as were set in the value="x" attribute. It only works on <form> elements (not the <span> you have at the moment) as it is a largely pointless wrapper for the JavaScript form.reset() method.

(One could potentially re-write resetForm to work on non-forms by manually setting value back to defaultValue on form fields, and the same with defaultChecked and defaultSelected. But that's not what it's doing at the moment.)

clearForm, on the other hand, sets the values of all inputs inside the element to blank, whatever element it is. But how can you remove the value of a <select> box? It doesn't make any sense; you can't have ‘no option selected’ in a single-select drop-down, unless it contains no options at all.

jQuery.form does seem to be trying to get ‘no option selected’, but it'll never work. line 610:

    else if (tag == 'select')
        this.selectedIndex = -1;

You can't set selectedIndex to -1. You could try changing this code to set it to 0 to select the first option instead. (This would still be insufficient for select-many boxes though.)

From this quick look at the jQuery.form code, my suspicion is that it isn't really very good.

bobince
A: 

Try this answer too: I second Pointy on the coding quality.

stackoverflow answer

Darryl Hebbes