/    Sign up×
Community /Pin to ProfileBookmark

A delete/reset function for forms

Hello!

I have a question regarding making a smart delete/reset function so I dont have to make one for every element in my form.

Different types of input, selectbox and check boxes will be shown depending on what the user have clicked on. For example if a user selects row 2 in my <select> a <div layer> with a couple of checkboxes will be shown (div style=” visibility” is set to visible);

But if this should work all elements that arent “visible” must be reset to their “default value” so the request will be correctly made.

So my question is lets say I have a function called delete() can I send an object that should be reset and in my delete() function detect which kind of form-element the retrieved object is of ,se below what I want to do:

<script…..>

function delete(myobj){
if(myobj.isTextfield){
document.getElementById(myobj.id).text=””;
document.getElementById(myobj.id).value=””
}

if(myobj.isSelectbox){
document.getElementById(myobj.id).selectedIndex[0]=true;
}

}

</script>

<form name=”myform”…>

<select name=”myselect”>
.
.
</select>

<input name=”mytext” type=”text”…>
</input>

</form>

I hope U understand what I want to achieve…maybe there is a simplier way to achieve this or?

Regards/D_S

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WaylanderOct 25.2005 — I wouldnt bother trying to keep track of these intial values, if the page movement is relying on javascript then just use javascript to make sure its right before it sends.

Let the user fill in what they like and keep your conditional display working, even leave the values in there incase they want to come back to a previous option that they had, and then make some code (you may already have some kind of checkForm() function in place) to check on submit which options are open, based on whats selected just submit the right values, or just set the unselected fields back to thier original value before you return true.

Waylander.
Copy linkTweet thisAlerts:
@konithomimoOct 25.2005 — For text boxes, checkboxes, and text areas:
<i>
</i>&lt;script type="text/javascript"&gt;

function ResetForm(which){
var pass=true
var first=-1
if (document.images){
for (i=0;i&lt;which.length;i++){
var tempobj=which.elements[i]
if (tempobj.type=="text"){
eval(tempobj.value="")
if (first==-1) {first=i}
}
else if (tempobj.type=="checkbox") {
eval(tempobj.checked=0)
if (first==-1) {first=i}
}
else if (tempobj.col!="") {
eval(tempobj.value="")
if (first==-1) {first=i}
}
}
}
which.elements[first].focus()
return false
}
&lt;/script&gt;


That is the reset function, and then to call it:
<i>
</i>&lt;form onSubmit="return ResetForm(this)" method="post" action="#" name=""&gt;


EDIT:

*Note - The function does not have to be called from the onSubmit of the form. You can call it from anywhere as long as you send it the name of the form that you want to reset.
Copy linkTweet thisAlerts:
@digital_stormauthorOct 25.2005 — Thanks! ?

/D_S


For text boxes, checkboxes, and text areas:
<i>
</i>&lt;script type="text/javascript"&gt;

function ResetForm(which){
var pass=true
var first=-1
if (document.images){
for (i=0;i&lt;which.length;i++){
var tempobj=which.elements[i]
if (tempobj.type=="text"){
eval(tempobj.value="")
if (first==-1) {first=i}
}
else if (tempobj.type=="checkbox") {
eval(tempobj.checked=0)
if (first==-1) {first=i}
}
else if (tempobj.col!="") {
eval(tempobj.value="")
if (first==-1) {first=i}
}
}
}
which.elements[first].focus()
return false
}
&lt;/script&gt;


That is the reset function, and then to call it:
<i>
</i>&lt;form onSubmit="return ResetForm(this)" method="post" action="#" name=""&gt;


EDIT:

*Note - The function does not have to be called from the onSubmit of the form. You can call it from anywhere as long as you send it the name of the form that you want to reset.[/QUOTE]
×

Success!

Help @digital_storm 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,
)...