0

In this visualforce page there are two inputs and one button. How to swap the values in given two inputs from input-1 to input-2 and input-2 to input-1. 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;
    }

}

Please provide suggestions to do this swap functionality

1 Answer 1

2

Once you do x=y; you're overwriting the value stored into x, so its previous value is lost, then y=x; just store into y its own value.
In order to swap values, you must save the value into a temporary variable:

String tmp = x;
x = y;
y = tmp;
1
  • Thank you very much ! Commented Dec 25, 2021 at 9:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .