/    Sign up×
Community /Pin to ProfileBookmark

Hey,

I have the simple test form below. When I enter something into Sale Order field, hit the submit button. Once the submitted button is clicked, I click on Reset Form button to clear out all fields. However the code below did not working, but if I removed the <fieldset> form the form below then it clear out the value from the form. Can anyone please help?

Thanks

<script type=”text/javascript”>
function clearForm(oForm) {

var elements = oForm.elements;

oForm.reset();

for(i=0; i<elements.length; i++) {

field_type = elements[i].type.toLowerCase();

switch(field_type) {

case “text”:
case “password”:
case “textarea”:
case “hidden”:

elements[i].value = “”;
break;

case “radio”:
case “checkbox”:
if (elements[i].checked) {
elements[i].checked = false;
}
break;

case “select-one”:
case “select-multi”:
elements[i].selectedIndex = -1;
break;

default:
break;
}
}

}

</script>

//FORM STARTS
<cfform action=”##” method=”post”>
<fieldset>
<legend>Ital Search Form</legend>
<ol>
<li>
<label for=”so”>Sales Order Number</label>
<cfinput id=”so” type=”text” name=”so” value=”#form.so#” size=”20″ />
</li>
</ol>
</fieldset>
<cfinput type=”submit” name=”submit” class=”button” value=”Search”/>
<cfinput type=”button” name=”reset_form” value=”Reset Form” onclick=”clearForm(this.form);”>
</cfform>

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsApr 14.2014 — using break in default will cause the for() to break. instead of the expected break on the switch().function clearForm(oForm){
var elements = oForm.elements;
oForm.reset();

<i> </i>for(var i = 0, size = elements.length; i &lt; size; ++i){
<i> </i> var field_type = elements[i].type.toLowerCase();

<i> </i> switch(field_type){
<i> </i> case "text":
<i> </i> case "password":
<i> </i> case "textarea":
<i> </i> case "hidden":
<i> </i> elements[i].value = '';
<i> </i> break;
<i> </i> case "radio":
<i> </i> case "checkbox":
<i> </i> if(elements[i].checked){
<i> </i> elements[i].checked = false;
<i> </i> }
<i> </i> break;
<i> </i> case "select-one":
<i> </i> case "select-multi":
<i> </i> elements[i].selectedIndex = -1;
<i> </i> break;
<i> </i> //default: // don't need default if it has nothing to run...
<i> </i> //break; // don't use break after default
<i> </i> }
<i> </i>}
}
Copy linkTweet thisAlerts:
@hongho2authorApr 15.2014 — Did not working of what i try to achieve. Does anyone have any ideas?
Copy linkTweet thisAlerts:
@Sup3rkirbyApr 15.2014 — It looks like you are using a [B][I]<cfform>[/I][/B] and [B][I]<cfinput>[/I][/B] tag, which isn't valid. A quick google search tells me it's something from ColdFusion, which I'm sure it's fine there but as far as any standard HTML/javascript goes they won't be valid and thus the script won't work.

To be fair though, I've never worked with ColdFusion so if that's what you are building this page in you can always verify by opening the page, pressing [B]F12[/B] and opening the '[I]Console[/I]' tab. When you click the 'Reset Form' button you will see any errors that occur when you script runs.

If I copy and paste your code and then make the changes with the two tags I mentioned earlier (changing them to their standard HTML tag) I get no errors and the script works. Hence why I think it's just an issue with invalid tags.
×

Success!

Help @hongho2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...