Swapping two text box values

I have two text box values:-

var pickup = $('#txt-pickup'); var destination = $('#txt-destination');

and I want to swap the two values as follows:-

pickup.val(destination.val()); destination.val(pickup.val());

However, the above will always set both values as the same value. (same as the destination value)

Is there a way of changing both of these at the same time?


I made a jsfiddle for you:

<input id="txt-pickup" type="text"/> <input id="txt-destination" type="text"/> <input id="change" type="button" value="Swap"/> $(document).ready(function (){ $("#change").on('click',function(){ var pickup = $('#txt-pickup').val(); $('#txt-pickup').val($('#txt-destination').val()); $('#txt-destination').val(pickup); }); });




Store the value in the variable than assign it to the another textbox.

var pickup = $('#txt-pickup'), destination = $('#txt-destination'); var d_val = destination.val(); //cache value of destination destination.val(pickup.val()); //change value of destination to pickup.val() pickup.val(d_val); //change value of pickup to cached value of destination


You can store the value of any of these two textbox in a variable and then set this variable as the value of the other textbox:

var tmp = $('#txt-pickup').val(); pickup.val(destination.val()); destination.val(tmp);

<strong>Fiddle Demo</strong>


Try this worked on me

<input type="text" id="text1"/>
<input type="text" id="text2"/>
<input type="button" id="button" value="swap"/>


    var a = $("#text1").val();





  • Loading a TGA File and Using it with OpenGL
  • C++ exception safety paranoia: how much is too much?
  • How do I create shared library using ld?
  • Selection Sort, For Java
  • Is there a way to clone native functions in javascript like window.alert or document.write
  • cell spacing in div table
  • How to clear text inside text field when radio button is select
  • Android fill_parent issue
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Does CUDA 5 support STL or THRUST inside the device code?
  • what is the difference between the asp.net mvc application and asp.net web application
  • jquery mobile loadPage not working
  • Properly structure and highlight a GtkPopoverMenu using PyGObject
  • Calling of Constructors in a Java
  • Traverse Array and Display in markup
  • How to format a variable of double type
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Transpose CSV data with awk (pivot transformation)
  • Matrix multiplication with MKL
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Why can't I rebase on to an ancestor of source changesets if on a different branch?
  • How do I configure my settings file to work with unit tests?
  • How to stop GridView from loading again when I press back button?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Busy indicator not showing up in wpf window [duplicate]
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal