Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.
<apex:page id="page1">
<apex:includeScript value="{!URLFOR($Resource.JqueryA, 'js/jquery-1.6.2.min.js')}"/>
<apex:includeScript value="{!URLFOR($Resource.JqueryA, 'js/jquery-ui-1.8.16.custom.min.js')}"/>
<apex:form id="form1">
<apex:inputtext value="{!text1}"/>
<apex:inputtext value="{!text2}"/>
<apex:commandButton value="Reset" onclick="myFunction();"/>
<apex:commandButton value="List" action="{!list}"/>
<script>
    function myFunction()
        {
        document.getElementById(form1).reset();
        alert("Sundhar");
        }
    </script>
</apex:page>

it want reset the form . but i can't able to reset it ?

share|improve this question

closed as off-topic by user320, LaceySnr Feb 25 at 5:13

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Programming questions not specific to Salesforce are off-topic here, but can be asked on Stack Overflow." – user320, LaceySnr
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Why do you want reset it? Or better question what do you want achieve by doing this? –  Artur Kępczyński Feb 11 at 7:40
    
when i click in the list for the time it is showing the results and i modify the textbox data and i click .it is throwing the error .so i want to reset the form the form to modify the search –  Teja Feb 11 at 7:47
    
Cant you just create apex method and put null in all fields in form? –  Artur Kępczyński Feb 11 at 7:49
    
i have two button are there first button is to reset and second button is to show list .when i click on the reset button before click the list it is reset the form when i click the reset button after clicking the list it is not working.i need that mainly it has to reset after clicking the list button –  Teja Feb 12 at 7:32

4 Answers 4

You were trying to fetch dom element by id but didn't used string literal in doing so. To retrieve an element with the id form1, you should place the string literal form1 in (double) quotes, like so:

function myFunction() {
    document.getElementById('form1').reset();
}


However, even that wouldn't have worked in your case, because it would fetch element with id form1, while your form would probably have composite id, which would include entire component tree above the form itself. I.e. in your case form1 is just ID of the VisualForce form component, not of the DOM element rendered in the browser. The id of the form element rendered in the browser would have probably looked something similar to page1;j0;g0;...;form1.

So, to recap, to fetch the reference to form DOM element, you would have to use DOM ID of the form, which you could easily do with $Component field. Like so:

function myFunction() {
    document.getElementById('{!$Component.form1}').reset();
}
share|improve this answer
    
it showing the alert but it is not sweeping the value in text box –  Teja Feb 11 at 10:00
    
i have two button are there first button is to reset and second button is to show list .when i click on the reset button before click the list it is reset the form when i click the reset button after clicking the list it is not working.i need that mainly it has to reset after clicking the list button –  Teja Feb 12 at 7:30

I think your problem is that you don't call your form properly from your javascript method try something like :

<apex:form>
<apex:inputtext value="{!text1}"/>
<apex:inputtext value="{!text2}"/>
<apex:commandButton value="Reset" onclick="this.form.reset();return false;"/>
<apex:commandButton value="List" action="{!list}"/>
</apex:page>
share|improve this answer
    
No Not working. –  Teja Feb 11 at 9:48
    
@Teja can you be more constructive? –  user320 Feb 11 at 15:18
    
i have two button are there first button is to reset and second button is to show list .when i click on the reset button before click the list it is reset the form when i click the reset button after clicking the list it is not working.i need that mainly it has to reset after clicking the list button –  Teja Feb 12 at 7:30

Since you already are using jquery:

Change this two lines:

<apex:form id="form1">

document.getElementById(form1).reset();

for this:

<apex:form id="js-form1">

$('form[id$="js-form1"]')[0].reset();
share|improve this answer
    
No Not Working . –  Teja Feb 11 at 9:49
    
I updated my sample –  Martin Borthiry Feb 11 at 14:28
    
@Teja can you be more constructive? –  user320 Feb 11 at 15:19
    
if you try where I've posted it should works –  Martin Borthiry Feb 11 at 16:26
    
i have two button are there first button is to reset and second button is to show list .when i click on the reset button before click the list it is reset the form when i click the reset button after clicking the list it is not working.i need that mainly it has to reset after clicking the list button –  Teja Feb 12 at 7:31

Try this in your function.

It uses jQuery. First step is to alias jQuery because using $ conflicts with other Salesforce libraries. Then we are going to use a selector to find the element that ends with form1. This returns an array, so we need just the first one in the array.

var j$ = jQuery.noConflict(); 
function myFunction() {
    j$("[id$='form1']")[0].reset();
}
share|improve this answer
    
i have two button are there first button is to reset and second button is to show list .when i click on the reset button before click the list it is reset the form when i click the reset button after clicking the list it is not working.i need that mainly it has to reset after clicking the list button –  Teja Feb 12 at 7:31
    
Then you can't do that with JavaScript. Your reset button will need to call an Apex method that nulls out all the values. The problem is that the list button is updating the page, so the Reset doesn't know what it was originally. –  Daniel Hoechst Feb 12 at 15:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.