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>` 
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
rojoartwork
  • 31
  • 1
  • 2
  • It's not valid markup to put a `` inside a ``. A `` can contain inline elements only, and `
    ` is a block-level element.
    – Pointy Feb 25 '10 at 14:53
  • thanks for the feedback, however it didn't reset after removing all table/tr/td tags and only having the select in the span. – rojoartwork Feb 25 '10 at 15:49

3 Answers3

1

There's a lot wrong with your code.

  1. You're declaring your "hideMe" function in a way that won't work in browsers other than IE, and the IE behavior it relies on is badly broken anyway. You're setting the function up as a jQuery "ready" handler, not a global function. What you should do instead is bind an anonymous function to the "click" event for your radio button. You can do that in the "ready" handler.

  2. The "clearForm" function — what is that? Where is it defined?

  3. As noted in the comment, putting a table inside a span is wrong. Use a <div> if you need a container for the table.

  4. To reset a form, there's a native "reset" function on <form> elements. That said, there is no <form> element in your page. It's not really correct to have input elements without a form element, and you won't be able to do a reset without a form anyway.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • 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 Feb 25 '10 at 15:55
1

$("#"+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
  • 528,062
  • 107
  • 651
  • 834
0

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

stackoverflow answer

Community
  • 1
  • 1
Darryl Hebbes
  • 998
  • 1
  • 9
  • 15