Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swapping two text box values

Tags:

jquery

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?

like image 429
nsilva Avatar asked Jun 05 '26 16:06

nsilva


1 Answers

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);


    });

});

JSFIDDLE

like image 79
Joakim M Avatar answered Jun 10 '26 19:06

Joakim M