I have created two VF page.
-
With input type button
-
With button tag like below
First VF page
<apex:page controller="testVFPageCtrl">
<apex:form id="frm">
{!string1}
<input type="button" onclick="actionFunction();" value=" click here " class="btn"/>
<apex:actionFunction name="actionFunction" action="{!testCallMethod}" rerender="frm"/>
</apex:form>
</apex:page>
Second VF page
<apex:page controller="testVFPageCtrl">
<apex:form id="frm">
{!string1}
<button onclick="actionFunction();" class="btn"> click here </button>
<apex:actionFunction name="actionFunction" action="{!testCallMethod}" rerender="frm"/>
</apex:form>
</apex:page>
For both controller
public class testVFPageCtrl
{
public String string1 {get; private set;}
public testVFPageCtrl()
{
string1 = '-----inside controller----';
}
public void testCallMethod()
{
string1 = '-----inside test call method----';
system.debug('=========method called=====');
}
}
So when I click on input Type button then page doesn't load simply rerender the form.
But when I Click on button tag. first it call the method i.e. testCallMethod
then page reloaded and my all variable reinitialize.
Just wanted to know why input button and button tag behave differently here.
And what is the reason button tag reload the page
As per my understanding looks like button tag try to submit the form.
Best Answer
The second VF page has "button" tag. But you didn't specify the type of the button. "button" tag has attribute "type", "type" attribute has 2 values
type="button"
type="submit"
actionFunction()
methodactionFunction()
method and submits the form.If you don't specify the type attribute, each browser handles it differently. You can refer here