How To Swap Value Between Two Input Fields Using Jquery

In this topic,

We are going to see how to swap values between two input fields using jQuery.

Clicking on the “>>” button will swap the value of the left input box to the Right input box, and the icon of the “>>” button will be “<<“.

Step-1: Include the jQuery link in the <head>..</head>tag on index.html file.

<script src=""></script>

Step-2: Create Html Code in the index.html file.

  <div class="row">
    <div class="first-box">
      <input type="text" name="box1" id="left">
    <div class="button">
      <button type="button" id="btnleftside">>></button>
    <div class="second-box">
      <input type="text" name="box1" id="right">

Step-3: Add jQuery code in <script>…</script> tag on index.html.

In This code, first of all I have declared two variables.
When I click on the button, the value of the first input box goes to the second input box and, the button text change.

 $("#btnleftside").click(function () {
     var left = $("#left").val();
     var right = $("#right").val();

     if ($("#btnleftside").text() == ">>") {
     else {

Review the below video.

I hope you guys found something useful  ??

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories