function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Hari nadh babu EluruHari nadh babu Eluru 

How to swap two values in visualforce page

Hi there,
In this visualforce page there are two inputs and one button name as swap. How the swap the values in given two inputs from input-1 to input-2. Please provide suggestions

I tried the above code
VF code:-
<apex:page controller="Swap_Values">
<apex:form >
<apex:outputLabel >Enter Value</apex:outputLabel>
<apex:inputText value="{!x}"/>
<apex:inputText value="{!y}"/>
<apex:commandButton value="Swap" action="{!SV}"/>
</apex:form>
</apex:page>
Apex code:-
public class Swap_Values {

    public String y { get; set; }

    public String x { get; set; }

    public PageReference SV() {
        x=y;
        y=x;
        return null;
    }

}


 
Best Answer chosen by Hari nadh babu Eluru
sfdc98sfdc98
Hi ,
try below code,

public class swapex {
    public string x {set;get;}
    public string y {set;get;}
    public pageReference sv(){
        string tmp=x;
        x=y;
        y=tmp;
        return null;
    }
}

vf:
<apex:page controller="swapex" >
    <apex:form >
<apex:outputLabel >Enter Value</apex:outputLabel>
<apex:inputText value="{!x}"/>
<apex:inputText value="{!y}"/>
<apex:commandButton value="Swap" action="{!sv}"/>
</apex:form>
</apex:page>

All Answers

sfdc98sfdc98
Hi ,
try below code,

public class swapex {
    public string x {set;get;}
    public string y {set;get;}
    public pageReference sv(){
        string tmp=x;
        x=y;
        y=tmp;
        return null;
    }
}

vf:
<apex:page controller="swapex" >
    <apex:form >
<apex:outputLabel >Enter Value</apex:outputLabel>
<apex:inputText value="{!x}"/>
<apex:inputText value="{!y}"/>
<apex:commandButton value="Swap" action="{!sv}"/>
</apex:form>
</apex:page>
This was selected as the best answer
Hari nadh babu EluruHari nadh babu Eluru
@sfdc98 Thank you !